当前位置:网站首页>Number precision-- use / instance
Number precision-- use / instance
2022-06-09 04:19:00 【It blade out of sheath】
Original website :number-precision-- Use / example _IT A blog with a sharp blade -CSDN Blog
brief introduction
explain
This article introduces with examples JavaScript library :number-precision Usage of .
number-precision Is an exact addition, subtraction, multiplication and division JavaScript library .
Official website
github:https://github.com/nefe/number-precision (Star Count :3.5K)
npm:https://www.npmjs.com/package/number-precision
Why use number-precision
reason :JS It is not accurate to calculate decimals
example :

All the questions
// Add
0.1 + 0.2 = 0.30000000000000004
0.2 + 0.4 = 0.6000000000000001
// Subtraction
1.5 - 1.2 = 0.30000000000000004
0.3 - 0.2 = 0.09999999999999998
// Multiplication
19.9 * 100 = 1989.9999999999998
9.7 * 100 = 969.9999999999999
// division
0.3 / 0.1 = 2.9999999999999996
0.69 / 10 = 0.06899999999999999
// Compare
0.1 + 0.2 === 0.3 // false
(0.3 - 0.2) === (0.2 - 0.1) // false
install
npm install number-precision --save
Method
import NP from 'number-precision'
NP.strip(num) // Turn to the nearest correct number
NP.plus(num1, num2, num3, ...) // Add :num + num2 + num3, Need at least 2 Parameters
NP.minus(num1, num2, num3, ...) // Subtraction :num1 - num2 - num3
NP.times(num1, num2, num3, ...) // Multiplication :num1 * num2 * num3
NP.divide(num1, num2, num3, ...) // division :num1 / num2 / num3
NP.round(num, ratio) // according to ratio integer
usage
import NP from 'number-precision'
NP.strip(0.09999999999999998); // = 0.1
NP.plus(0.1, 0.2); // = 0.3, Instead of the original wrong :0.30000000000000004
NP.plus(2.3, 2.4); // = 4.7, Instead of the original wrong :4.699999999999999
NP.minus(1.0, 0.9); // = 0.1, Instead of the original wrong :0.09999999999999998
NP.times(3, 0.3); // = 0.9, Instead of the original wrong :0.8999999999999999
NP.times(0.362, 100); // = 36.2, Instead of the original wrong :36.199999999999996
NP.divide(1.21, 1.1); // = 1.1, Instead of the original wrong :1.0999999999999999
NP.round(0.105, 2); // = 0.11, Instead of the original wrong :0.1
Be careful
number-precision Is not widely used , Not as good as big.js, bignumber.js, decimal.js.
meanwhile ,number-precision There are very few CDN Provide .
边栏推荐
- Rigidbody2d rotate around a fixed point rotate
- 17billion parameters, 28 open test sets SOTA, the industry's largest unified visual multi task model
- MySQL:常用的统计、分组统计、时间格式转换
- big.js、bignumber.js 和 decimal.js 的区别
- 150 basic acoustic knowledge that acoustic engineers should know (full text)
- golang---各个类型变量的比较运算
- 数据库连接问题,换版本后无法获取连接
- proteus仿真Arduino
- 测试网站搭建+渗透+审计之第二篇渗透测试
- Expansion chip, hisi3559av100 I2C debugging
猜你喜欢

golang---並發runtime包

(6)事件

Graph to document function based on pyqt5

Mysql 查询数据库中哪个表的字段个数最多

Give an example to illustrate the cell and num of lstmcell in TF_ What does unit mean

Golang-- concurrent runtime package

golang ---image--热力图与照片的重叠

Harbor容器安装以及相关特性部署与使用(SSL证书+AD域)

My creation anniversary

Openinfra Foundation launched the "targeted fund" program to promote successful open source governance experience
随机推荐
P5321 [bjoi2019] farewell (LCT)
Expansion chip, hisi3559av100 I2C debugging
Pdf splitting based on pyqt5
Golang--- redis operation
opcv图像二值化处理
Attention OCR Chinese version mas # ter Code Running Logic
golang---变量与其内容的存放
Rigidbody2d SweepTest Rigidbody2D.Cast 但用于rotation 旋转
软件测试(二)
基于PyQt5完成的PDF合并
上网行为组网
5. Quick (Group) sort
状态模式模拟电梯运行
(7)属性绑定
知识点滴- BC和BCE的区别
Golang ---image-- overlap of thermal maps and photos
MySQL中的多表查询
PHP e签宝电子签名Saas API 对接流程
(2) v-cloak指令
测试网站搭建+渗透+审计之第一篇使用IDEA启动网站