直播带货源码,js实现整数和小数分开并添加不同的样式

直播带货源码,js实现整数和小数分开并添加不同的样式

1.思路

1. 提前写好整数部分和小数部分class样式

2. 再把数值转换成字符串形式,split() 方法用分隔符把整数和小数部分分割成数组

3. 处理应对接口返回数值的不同情况,考虑周全

2.代码实现

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

​以上就是直播带货源码,js实现整数和小数分开并添加不同的样式, 更多内容欢迎关注之后的文章

 

posted @ 2022-10-31 14:14  云豹科技-苏凌霄  阅读(41)  评论(0)    收藏  举报