当前位置:网站首页>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'其他方法
还支持:
- 比较大小
- 绝对值
- 取模
- 四舍五入
- ...
边栏推荐
- Library management system 1- project approval
- 337. house raiding III
- Promise. All capture error
- Luogu p1091 chorus formation (longest ascending subsequence)
- SQL query. Only the column name is displayed but not the data
- Detailed explanation of mutationobserver
- 微信小程序开发(原生和uniapp)DOM标签对比介绍
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- Menu double linkage effect in uniapp
- es5和es6的学习小记
猜你喜欢

洛谷P1091合唱队形(最长上升子序列)

Deep Attentive Tracking via Reciprocative Learning
![pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])

Biweekly investment and financial report: capital rush yuan universe game

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员

Modular notes

迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)

byte和bit的区别

First day of database

A highly controversial issue
随机推荐
Dynamically change the direction of this
【Matlab印刷字符识别】OCR印刷字母+数字识别【含源码 1861期】
Shangtang technology has actively resumed work and will vigorously invest in the capacity and deployment of the digital sentry
Leetcode-647. Palindromic Substrings
News web page display
Education expert wangzhongze solves students' problems with one move
Duality-Gated Mutual Condition Network for RGBT Tracking
Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
Summary of string processing skills II
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
Explain the difference between void 0 and undefined
[Xunwei dry goods] opencv test of Godson 2k1000 development board
Graph Attention Tracking
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
12. integer to Roman numeral
Starting from scratch (V) realize bullet positioning and animation
Promise. All capture error
服务器调参实录
matplotlib的cmap
并发工具类