当前位置:网站首页>big.js--使用/实例
big.js--使用/实例
2022-06-11 07:06:00 【IT利刃出鞘】
原文网址:big.js--使用/实例_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍JavaScript的数学计算库:big.js。
Big.js 是一个小型,快速的 JavaScript 库,用于任意精度的十进制算术。
官网
文档:https://mikemcl.github.io/big.js/
github:https://github.com/MikeMcl/big.js (star数:4K)
npm地址:big.js - npm
big.js、bignumber.js、decimal.js
big.js、bignumber.js、decimal.js都是同一个作者写的,它们的区别见:big.js、bignumber.js 和 decimal.js 的区别_IT利刃出鞘的博客-CSDN博客
big.js特性
- 简单的API
- 比Java的BigDecimal的JavaScript版本更快、更小、更易于使用
- 仅6KB大小
- 复制了JavaScript数字的toExponential、toFixed和toPrecision方法
- 用可访问的十进制浮点格式存储值
- 全面的文档和测试集
- 没有依赖
- 使用ECMAScript 3,因此适用于所有浏览器
安装
该库是单个 JavaScript 文件:big.js或 ES 模块big.mjs。
浏览器
将 Big 添加到全局范围:
<script src='path/to/big.js'> </script>
ES模块:
<script type='module'>
import Big from './path/to/big.mjs';
从 CDN 获取最小版本:
<script src='https://cdn.jsdelivr.net/npm/[email protected]/big.min.js'> </script>
快速的CDN:
<script src="https://cdn.bootcdn.net/ajax/libs/big.js/6.2.0/big.js"></script>Node.js
$ npm install big.js
CommonJS:
const Big = require('big.js');
ES module:
import Big from 'big.js';
Deno
import Big from 'https://raw.githubusercontent.com/mikemcl/big.js/v6.0.0/big.mjs';
import Big from 'https://unpkg.com/[email protected]/big.mjs';
实例
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>This is title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/big.js/6.2.0/big.js"></script></head>
<body>
<div class="container">
这是div元素
</div>
<script>
const x = new Big(0.1);
const y = x.plus(0.2); // 0.3
const z = Big(0.7).plus(x).plus(y).toFixed(2); // 1.1
console.log(y.toString());
console.log(z.toString());
</script>
</body>
</html>
结果:

加减乘除
加
0.1 + 0.2 // 0.30000000000000004
const x = new Big(0.1);
const y = x.plus(0.2); // 0.3
Big(0.7).plus(x).plus(y).toFixed(2); // 1.1减
0.3 - 0.1 // 0.19999999999999998
const x = new Big(0.3);
const y = x.minus(0.1) // 0.2
(Big(0.7).minus(x).minus(y).toFixed(2) // 0.2乘
0.6 * 3 // 1.7999999999999998
x = new Big(0.6)
y = x.times(3) // '1.8'
Big('7e+500').times(y) // '1.26e+501'除
x = new Big(355)
y = new Big(113)
x.div(y) // '3.14159292035398230088'
Big.DP = 2
x.div(y) // '3.14'
x.div(5) // '71'其他方法
还支持:
- 比较大小
- 绝对值
- 取模
- 四舍五入
- ...
边栏推荐
- Shell脚本之启动Nacos服务端
- Promises/a+ standard Chinese Translation
- VTK-vtkPlane和vtkCutter使用
- Atom, the top stream editor, will leave the historical stage on December 15
- 1266_FreeRTOS调度器启动代码实现分析
- First day of database
- [MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
- 337. house raiding III
- News web page display
- [matlab printed character recognition] OCR printed letter + number recognition [including source code 1861]
猜你喜欢

Esp32 learning notes (49) - esp-wifi-mesh interface use
![[deploy private warehouse based on harbor] 3 deploy harbor](/img/cd/be68a430e86b4b23ad93b42a338f00.jpg)
[deploy private warehouse based on harbor] 3 deploy harbor

matplotlib的cmap

Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)

Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation

Flutter 约束容器组件

WPF data binding (IV)

News web page display

Education expert wangzhongze solves students' problems with one move

Starting from scratch (V) realize bullet positioning and animation
随机推荐
Stack -- one of two common linear structures of linear structure
教育专家王中泽老师:家庭教育重在自己成长
213. house raiding II
22年五月毕设
河南高考VS天津高考(2008年-2021年)
Common troubleshooting tools and analysis artifacts are worth collecting
Aircraft war from scratch (II) simple development
Graph Attention Tracking
CMAP of Matplotlib
通过 Ingress 进行灰度发布
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试
Grayscale publishing through ingress
Library management system 1- project approval
Latex various arrows / arrows with text labels / variable length arrows
【Matlab印刷字符识别】OCR印刷字母+数字识别【含源码 1861期】
VTK vtkplane and vtkcutter use
.NET C#基础(6):命名空间 - 有名字的作用域
Summary of string processing skills III
Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING