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>