Vue numbers increase from 0 to increase the animation, calculate the number of decimal places and js decimal multiplication, there are many decimals.

1. Vue realizes digital incremental animation effect

If the vue project wants to increase the number from 0, you can use the digital scrolling plugin—vue-count-to

Install

npm install vue-count-to

Example: page usage

<template>
  <countTo 
  	:startVal='startVal' //初始值
  	:endVal='endVal' //最终值
  	:duration='3000' //时长
  	:decimals="2" //这个是显示代表几位小数
  ></countTo>
</template>
 
<script>
  import countTo from 'vue-count-to';
  export default {
    
    
    components: {
    
     countTo },
    data () {
    
    
      return {
    
    
        startVal: 1000,
        endVal: 2017
      }
    }
  }
</script>

2. Determine how many decimal places the number has

    calcXiaoshu (num) {
    
    
      console.log(num)
      if (String(num).indexOf('.') == '-1') {
    
    
        return 0
      } else {
    
    
        var x = String(num).indexOf('.') + 1 //小数点的位置
        var y = String(num).length - x //小数的位数
        console.log(y)
        return y
      }
    },
    console.log(this.calcXiaoshu(1.23))//2

3. Solve the problem of many digits after performing multiplication of decimals

//第一个参为要乘的数字,第二个参是乘以几
    mul (num1, num2) {
    
    
      if (
        parseFloat(num1).toString() == 'NaN' ||
        parseFloat(num2).toString() == 'NaN'
      )
        return
      var m = 0,
        s1 = num1.toString(),
        s2 = num2.toString()
      try {
    
    
        m += s1.split('.')[1].length
      } catch (e) {
    
    
        console.log(e)
      }
      try {
    
    
        m += s2.split('.')[1].length
      } catch (e) {
    
    
        console.log(e)
      }
      return (
        (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) /
        Math.pow(10, m)
      )
    }
    console.log(12.34,100) //1234

Guess you like

Origin blog.csdn.net/TKP666/article/details/130122031