js实现整数和小数分开,并分别显示不同样式

1、需求

商品价格是8.01,需要将整数部分8设置字体大小为21px,小数部分01设置字体为14px

效果:

2、思路

1. 提前写好整数部分和小数部分class样式
2. 再把数值转换成字符串形式,split() 方法用分隔符把整数和小数部分分割成数组
3. 处理应对接口返回数值的不同情况,考虑周全 

 3、代码实现

<template>
  <view>
     <text class="intPrice">{{ intPrice }}</text>
     <text class="floatPrice">{{ floatPrice }}</text>
  </view>
</template>
<script>
   data(){
     return{
      floatPrice:''        
     } 
   },
   computed:{
     intPrice() {
      const str = this.detail.price + ''  //转成字符串
      const arr = str.split('.') //使用分隔符分割字符串成数组
      console.log(arr, 'arr')  // ["8", "01"] 
      let tempStr = arr[1] ? arr[1] : '00' //如果小数点后有值就用该值,没有默认'00'
      this.floatPrice = tempStr.length == 1 ? tempStr + '0' : tempStr //小数点后只有一位的话,补0
      return arr[0] + '.'
    }
 }
</script>
<style lang="scss" scoped>
    .intPrice {
      font-size: 21px;
      font-weight: bold;
    }
    .floatPrice {
      font-size: 14px;
      font-weight: bold;
    }
 </style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值