Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
A library of string four operations, which supports operations such as format output of thousandth decimal point
(一个字符串四则运算的库, 可以解决前端数字计算精度问题, 支持千分位小数点格式化输出等操作)
npm install a-calc
支持的运算符: + - * / %
纯计算:
import a_calc from "a-calc"
const { calc } = a_calc
calc("0.1 + 0.2")
calc("-0.1 + -0.2 * 0.3")
let a = 0.1
let b = 0.2
calc`1 + ${a} - ${b}`
格式化(千分位, 小数点控制):
import a_calc from "a-calc"
const { calc } = a_calc
// 千分位
calc("100000 + 100 | ,") // 竖线分割计算式和格式化字符串, 左边是计算式右边是格式化字符串
// 小数点支持 = <= >= 这三个格式化标识
calc("1 + 1 | =2") // 保留2位小数
calc("10.001 - 0.001 | <=2") // 小数位小于等于2, 如果本身小于等于2就原样输出, 如果大于2就取2
calc("100.00001 | >=2") // 小数位大于等于2, 如果大于2位就原样输出, 如果小于2 就补齐2位
// 千分位和小数位数的格式化可以混写不分顺序
calc("100.000001 | = 2 ,")
单数字格式化
正常来说 calc 的字面意思就是计算一个值, 格式化的标识是在计算之后顺便做的一个事情, 但是有些时候希望对单个的数字进行格式化, 这种情况其实也可以使用 calc
calc("10000 | ,")
这也是可行的, 可以认为一个数字的格式化也是经过计算得出的, 这种说法就没有什么问题, 但是如果你真的需要一个更好的语义那么可以通过如下的方式, 但是 calc 和 fmt 在做格式化的时候区别主要是名称的不同而已
import a_calc from "a-calc"
const { fmt } = a_calc
fmt("10000.000001 | ,")
fmt("0.123456 | =2")
fmt("0.123456 | =2,")
calc("(1) * 2 / (-0.001)")
这是严厉禁止的, 虽然实现对这个写法的支持使比较简单的, 但是我认为这种写法完全没有必要, 而且还要多写逻辑FAQs
A very powerful and easy-to-use number precision calculation and formatting library.
The npm package a-calc receives a total of 17,342 weekly downloads. As such, a-calc popularity was classified as popular.
We found that a-calc demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.