栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

bignumber.js--使用/实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

bignumber.js--使用/实例

原文网址:bignumber.js--使用/实例_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文介绍JavaScript的数学计算库:bignumber.js。

官网

文档:https://mikemcl.github.io/bignumber.js/

github:https://github.com/MikeMcl/bignumber.js

npm地址:https://www.npmjs.com/package/bignumber.js

bignumber.js特性
  1. 整数和小数
  2. 简单的API,但功能齐全
  3. 比Java的BigDecimal的JavaScript版本更快,更小,更容易使用
  4. 缩小到8 KB
  5. 复制toExponential,toFixed,toPrecision和toStringJavaScript的数字类型的方法
  6. 包含toFraction和正确舍入的squareRoot方法
  7. 支持加密安全的伪随机数生成
  8. 没有依赖
  9. 广泛的平台兼容性:仅使用JavaScript 1.5(ECMAScript 3)功能
  10. 全面的文档和测试集
安装

浏览器

ES module

Node.js

安装:npm install bignumber.js
使用:const BigNumber = require('bignumber.js');

ES module

import BigNumber from "bignumber.js";
// or maybe
import { BigNumber } from "bignumber.js";
// else
import { BigNumber } from "./node_modules/bignumber.js/bignumber.mjs";

Deno

import BigNumber from 'https://raw.githubusercontent.com/mikemcl/bignumber.js/v9.0.2/bignumber.mjs';
import BigNumber from 'https://unpkg.com/bignumber.js@latest/bignumber.mjs';
实例

代码





    
    This is title
    


    这是div元素






结果

加减乘除

​加

const a = 0.1 + 0.2                         // 0.30000000000000004
const x = new BigNumber(0.1);               // 0.1
const y = x.plus(0.2);                      // 0.3
const z = BigNumber(0.7).plus(x).plus(y).toFixed(2);  // 1.10

减

0.3 - 0.1                                 // 0.19999999999999998
const x = new BigNumber(0.3);
const y = x.minus(0.1)                    // 0.2
const z = BigNumber(0.7).minus(x).minus(y).toFixed(2);  // 0.20

乘

const a = 0.6 * 3                            // 1.7999999999999998
const x = new BigNumber(0.6)                 // 0.6
const y = x.times(3)                         // '1.8'
const z = BigNumber('7e+500').times(y)       // '1.26e+501'

除

const x = new BigNumber(355)
const y = new BigNumber(113)
x.div(y)                                 // '3.14159292035398230088'
x.div(5)                                 // '71'

BigNumber.config({ DECIMAL_PLACES: 2 })
const z = x.div(y)                         // '3.14'
其他方法

见:https://mikemcl.github.io/bignumber.js/
​

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979384.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号