css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<article id="arithmetic" class="arithmetic-container">
  <h1>add, subtract, multiply,divide </h1>
  <h1>{{message}}</h1>
  <section class="group">
    <h3>1、加:</h3>
    <p class="tips">如0.1+0.7=0.7999999999999999</p>
    <p>
      <input type="text" v-model="addition.num01"/> 
      + 
      <input type="text" v-model="addition.num02"/> 
      =
      <span>{{sum}}</span>
    </p>
    <p class="tips">未处理结果:{{parseFloat(addition.num01) + parseFloat(addition.num02)}}</p>
  </section>
  <section class="group">
    <h3>2、减:</h3>
    <p class="tips">如1.0-0.9=0.09999999999999998</p>
    <p>
      <input type="text" v-model="substraction.num01"/> 
      -  
      <input type="text" v-model="substraction.num02"/> 
      =
      <span>{{difference}}</span>
    </p>
    <p class="tips">未处理结果:{{parseFloat(substraction.num01) - parseFloat(substraction.num02)}}</p>
  </section>
  <section class="group">
    <h3>3、乘:</h3>
    <p class="tips">如0.2*0.8=0.16000000000000003</p>
    <p>
      <input type="text" v-model="multiplication.num01"/> 
      *  
      <input type="text" v-model="multiplication.num02"/> 
      =
      <span>{{product}}</span>
    </p>
    <p class="tips">未处理结果:{{parseFloat(multiplication.num01) * parseFloat(multiplication.num02)}}</p>
  </section>
  <section class="group">
    <h3>4、除:</h3>
    <p class="tips">如0.6/0.1=5.999999999999999</p>
    <p>
      <input type="text" v-model="division.num01"/> 
      ÷  
      <input type="text" v-model="division.num02"/> 
      =
      <span>{{quotient}}</span>
    </p>
    <p class="tips">未处理结果:{{parseFloat(division.num01) / parseFloat(division.num02)}}</p>
  </section>
</article>
            
          
!
            
              html{
  font-size: 62.5%;  
}
body{
  color: #666;
  line-height: 1.6;
  // font-size: 62.5%;
}
h1, h2, h3{
  font-weight: normal;  
}
input{
  padding: .5rem;
  width: 5em;
  border: 1px solid #dbdbdb;
}
.arithmetic-container{
  padding: 2rem;
  max-width: 100rem;
  margin: 0 auto;
  font-size: 1.4rem;
}
.tips{
  color: #999;
}
.group{
  margin: 1rem auto;
  padding: 1rem;
  border: 1px dashed #efefef;
}
ul.list{
  li{
    display: block;
    color:#999;
    &.active{
      color:#f00;
    }
  }
}
            
          
!
            
              var app = new Vue({
  el: '#arithmetic',
  data: {
    message: '解决基本四则运算的浮点数精度问题',
    addition:{
      num01: 0.1,
      num02: 0.7,
      // sum: 0
    },
    sum: 0,
    
    substraction:{
      num01: 1,
      num02: 0.9,
      // difference: 0
    },
    difference: 0,
    
    multiplication:{
      num01: 0.2,
      num02: 0.8,
      // product: 0
    },
    product: 0,
    
    division:{
      num01: 0.6,
      num02: 0.1,
      // quotient: 0
    },
    quotient: 0,
    list:[
      {
      'text':'11'
    },{
      'text':'12'
    },{
      'text':'13'
    },{
      'text':'14'
    },
         ],
    timer: 0,
    listIndex: 0,
    cycle: 0,
    speed: 500
  },
  computed: {
    sum: function() {
      // 解决浮点数精度问题:如0.1+0.7=0.7999999999999999
      return this.additionFun(this.addition.num01, this.addition.num02)
    },
    difference: function() {
      // 解决浮点数精度问题:如1.0-0.9=0.09999999999999998
      return this.substractionFun(this.substraction.num01, this.substraction.num02)
    },
    product: function() {   
      // 解决浮点数精度问题:如0.2*0.8=0.16000000000000003
      return this.multiplicationFun(this.multiplication.num01, this.multiplication.num02)
    },
    quotient: function() {
      // 解决浮点数精度问题:如0.6/0.1=5.999999999999999
      return this.divisionFun(this.division.num01, this.division.num02)
    }
  },
  methods: {
    // 处理浮点数的精度,避免计算误差
    // 加法
    additionFun: function(num01, num02) {
      var r1, r2, m
      try {
        r1 = num01.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = num02.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      m = Math.pow(10, Math.max(r1, r2))
      return Math.round(num01 * m + num02 * m) / m
    },
    // 减法
    substractionFun: function(num01, num02) {
      var r1, r2, m, n
      try {
        r1 = num01.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = num02.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      m = Math.pow(10, Math.max(r1, r2))
      n = (r1 >= r2) ? r1 : r2
      return ((num01 * m - num02 * m) / m).toFixed(n)
    },
    // 乘法
    multiplicationFun: function(num01, num02) {
      var r1, r2, m
      r1 = num01.toString()
      r2 = num02.toString()
      m = 0
      try {
        m = m + r1.split('.')[1].length
      } catch (e) {
      }
      try {
        m = m + r2.split('.')[1].length
      } catch (e) {
      }
      return Number(r1.replace('.', '')) * Number(r2.replace('.', '')) / Math.pow(10, m)
    },
    // 除法
    divisionFun: function(num01, num02) {
      var t1, t2, r1, r2
      try{
        t1 = num01.toString().split('.')[1].length
      } catch (e){
        t1 = 0
      }
      try{
        t2 = num02.toString().split('.')[1].length
      } catch (e){
        t2 = 0
      }
      // with (Math) {
        r1 = Number(num01.toString().replace('.', ''))
        r2 = Number(num02.toString().replace('.', ''))
        return this.multiplicationFun((r1/r2), Math.pow(10, t2 - t1))
      // } 
    },
    rolling(time) {
      const that = this
      that.timer = setTimeout(function(){
        console.log(this.speed)
        if(that.listIndex < that.list.length){
          that.listIndex++
        } else {
          that.listIndex = 0
          that.cycle++
        }       
        if(that.cycle > that.list.length * 20){
          clearTimeout(that.timer)
          // alert('stop!')
          console.log('stop')
          that.cycle = 0
        } else {
          that.speed = that.speed + 50
          that.rolling(that.speed)
        }
      },time)
    }
  }
}
);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console