精確加減乘除

1. 前言

  1. 單個(gè)功能一篇發(fā)展也方便自己使用
  2. 加減乘除 是常見(jiàn)的功能,但是經(jīng)常會(huì)遇到不精準(zhǔn)的問(wèn)題,
    典型的比如 0.2+ 0.1 === 0.3 問(wèn)題

2. 加法代碼

  // 加法封裝 add(0.1, 0.2, 0.3, 0.4) => 1??梢詡鞫鄠€(gè)參數(shù)進(jìn)行相加
 const add = (...val)=> {
    let max = 0;
    let count = 0;
    for (let i = 0; i < val.length; i++) {
      const strVal = val[i].toString();
      const index = strVal.indexOf(".");
      let num = 0;
      if (index > -1) {
        num = strVal.length - 1 - index;
        max = num > max ? num : max;
      }
    }
    for (let i = 0; i < val.length; i++) {
      count += Math.round(val[i] * Math.pow(10, max));
    }
    return count / Math.pow(10, max);
  },
     

3. 減法

  // 減法封裝  sub(1, 0.2, 0.3, 0.4) => 0.1。相當(dāng)于1 - 0.2 -0.3 -0.4;可以傳多個(gè)參數(shù),使用首位減后面的所有參數(shù)。
 const  sub = (...val) =>{
    let sum,
      maxDecimalLength = getMaxDecimalLength(...val);
    val.forEach((x, index) => {
      let nurVal = Math.round(x * Math.pow(10, maxDecimalLength));
      if (index === 0) sum = nurVal;
      else sum -= nurVal;
    });
    return sum / Math.pow(10, maxDecimalLength);
  },
  1. getMaxDecimalLength 方法 獲取小數(shù)位數(shù)在下面有寫(xiě)

4. 乘法

  // 乘法ride(0.5, 0.6) => 3, 只允許傳入兩個(gè)參數(shù)。%計(jì)算可以這樣ride(0.5, 100) => 50。
  ride(...val) {
    const strVal = val[0].toString();
    const strValTwo = val[1].toString();
    const index = strVal.indexOf(".");
    const indexTwo = strValTwo.indexOf(".");
    const num = [0, 0];
    if (index > -1) {
      num[0] = strVal.length - 1 - index;
    }
    if (indexTwo > -1) {
      num[1] = strValTwo.length - 1 - index;
    }
    return (
      Math.round(
        val[0] * Math.pow(10, num[0]) * (val[1] * Math.pow(10, num[1]))
      ) / Math.pow(10, num[0] + num[1])
    );
  }

5. 除法

  // 除法exc(0.5, 0.2) => 2.5, 只允許傳入兩個(gè)參數(shù)。如果計(jì)算出現(xiàn)無(wú)窮數(shù)請(qǐng)后期根據(jù)需要修改最后代碼進(jìn)行取舍。
  exc(val, valTwo = 100) {
    const strVal = val.toString();
    const strValTwo = valTwo.toString();
    const index = strVal.indexOf(".");
    const indexTwo = strValTwo.indexOf(".");
    const num = [0, 0];
    if (index > -1) {
      num[0] = strVal.length - 1 - index;
    }
    if (indexTwo > -1) {
      num[1] = strValTwo.length - 1 - index;
    }
    return (
      Math.round(val * Math.pow(10, num[0])) /
      (Math.round(valTwo * Math.pow(10, num[1])) *
        Math.pow(10, num[0] - num[1]))
    );
  }

6. 獲取小數(shù)位數(shù)

 /*
   * 獲取小數(shù)位數(shù)
   */
  getMaxDecimalLength(...val) {
    // 最大小數(shù)位長(zhǎng)度
    let maxDecimalLength = 0;
    val.forEach((x) => {
      const strVal = x.toString(),
        dotIndex = strVal.indexOf(".");
      if (dotIndex > -1) {
        // 獲取當(dāng)前值小數(shù)位長(zhǎng)度
        let curDecimalLength = strVal.length - 1 - dotIndex;
        if (curDecimalLength > maxDecimalLength) {
          maxDecimalLength = curDecimalLength;
        }
      }
    });
    return maxDecimalLength;
  },

參考資料


初心

我所有的文章都只是基于入門(mén),初步的了解;是自己的知識(shí)體系梳理,如有錯(cuò)誤,道友們一起溝通交流;
如果能幫助到有緣人,非常的榮幸,一切為了部落的崛起;
共勉
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容