最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

前端vue項目如何使用Decimal.js做加減乘除求余運算

 更新時間:2024年05月28日 08:59:23   作者:H_HX126  
decimal.js是使用的二進制來計算的,可以更好地實現(xiàn)格化式數學運算,對數字進行高精度處理,使用decimal類型處理數據可以保證數據計算更為精確,這篇文章主要給大家介紹了關于前端vue項目如何使用Decimal.js做加減乘除求余運算的相關資料,需要的朋友可以參考下

1 vue項目安裝Decimal

npm install decimal.js

2 注意

運算結果是Decimal對象,需要使用.toNumber()轉為數字

3 加 add

const Decimal = require('decimal.js')
const num1 = Decimal("5");
const num2 = Decimal("3");
const remainder = num1.add(num2);

4 減 sub

const Decimal = require('decimal.js')
const num1 = Decimal("5");
const num2 = Decimal("3");
const remainder = num1.sub(num2);

5 乘 mul

const Decimal = require('decimal.js')
const num1 = Decimal("5");
const num2 = Decimal("3");
const remainder = num1.mul(num2);

6 除 div

const Decimal = require('decimal.js')
const num1 = Decimal("5");
const num2 = Decimal("3");
const remainder = num1.div(num2);

7 求余 modulo

const Decimal = require('decimal.js')
const num1 = Decimal("5");
const num2 = Decimal("3");
const remainder = num1.modulo(num2);

附:vue 使用decimal.js 解決小數相加合計精確度丟失問題

  • 安裝依賴 decimal.js
    • npm install --save decimal.js
  • 封裝
    • 在utils文件夾下創(chuàng)建decimal.js文件
import { Decimal } from 'decimal.js'
export function add (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        y = 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.plus(yy).toNumber()
}
// 減
export function sub (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        y = 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.sub(yy).toNumber()
}
// 除
export function div (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        return 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.div(yy).toNumber()
}
//乘
export function mul (x, y) {
    if (!x) {
        x = 0
    }
    if (!y) {
        y = 0
    }
    const xx = new Decimal(x)
    const yy = new Decimal(y)
    return xx.mul(yy).toNumber()
}
  • 頁面使用
<script>
  import {add} from "@/utils/decimal"
  export default {
    methods:{
      handlePlus() {
        add(10.5,4.877)
      }
    }
  }
</script>

總結 

到此這篇關于前端vue項目如何使用Decimal.js做加減乘除求余運算的文章就介紹到這了,更多相關vue Decimal.js做加減乘除求余運算內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

当雄县| 通海县| 曲靖市| 普格县| 丹江口市| 钦州市| 大冶市| 石河子市| 乌兰县| 上思县| 黄梅县| 永善县| 山东| 渑池县| 孟津县| 陈巴尔虎旗| 新乡市| 旬阳县| 平顶山市| 天全县| 安吉县| 金溪县| 花垣县| 潞城市| 清苑县| 湖南省| 新平| 叶城县| 永登县| 杨浦区| 化州市| 曲周县| 来宾市| 乌苏市| 汤原县| 公安县| 湛江市| 遂昌县| 石家庄市| 饶河县| 甘洛县|