Fox Big Decimal
大数操作(加、减、乘、除、格式化)的工具类.
Install
NPM
npm i @fox-js/big-decimal -S
YARN
yarn add @fox-js/big-decimal
使用指南
直接在代码中引入模块即可,模块会自动完成初始化并生效
import '@fox-js/big-decimal'
乘
multiply(x, y)
import { multiply } from '@fox-js/big-decimal'
let n = multiply(336662, 3)
console.info(`multiply:${n}`)
除
divide(x, y, precision = 8)
import { divide } from '@fox-js/big-decimal'
n = divide(36223, 3, 2)
console.info(`divide:${n}`)
加
add(x, y)
import { divide } from '@fox-js/big-decimal'
n = add(3533, 15)
console.info(`add:${n}`)
减
subtract(x, y)
import { subtract } from '@fox-js/big-decimal'
n = subtract(122223, 22321312)
console.info(`subtract:${n}`)
四舍五入
round(number, precision, roundingMode)
按指定的位数对数值进行四舍五入
import { round } from '@fox-js/big-decimal'
round('123.678', 2)
round('123.657', 1, RoundingModes.DOWN)
round('123.657', 2, RoundingModes.CEILING)
RoundingMode
Round also supports the following rounding modes
- CEILING - Rounding mode to round towards positive infinity.
- DOWN - Rounding mode to round towards zero.
- FLOOR - Rounding mode to round towards negative infinity.
- HALF_DOWN - Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
- HALF_EVEN - Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
- HALF_UP - Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
- UNNECESSARY (!Not Implemented!)- Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
- UP - Rounding mode to round away from zero.
floor
floor(number)
Returns the whole number nearest but not greater than the input number.
floor(12.8)
floor(-12.3)
ceil
ceil(number)
Returns the whole number nearest but not lesser than the input number.
ceil(12.8)
ceil(-12.3)
negate
Returns negation of a given number.
negate('123.678')
negate('-1234')
对比
numCompareTo(number1, number2)
比较两个数据,Returns 1, 0 and -1 if number1 > number2, number1 == number2 and number1 < number2 respectively.
compareTo('23.678', '67.34')
compareTo('23.678', '23.6780')
compareTo('123.678', '67.34')
判断数字是否在范围内
inScope(number, min, max)
inScope('1', '1', '5')
inScope('3', '1', '10')
对数字进行扩展
scaleNumber(value, scale)
如果 scale>0 代表 multiply(value,scale)
如果 scale<0 代表 divide(value, negate(scale))
scaleNumber('100', '5')
scaleNumber('100', '-5')
数据格式化
numberFormat(value, decimalLength = -1, autoPadding = false, digits = 3, separator = ',')
- value 待格式化 value
- decimalLength(小数长度)
- autoPadding(是否自动填充 0)
- digits(分块长度)
- separator(分块隔离符号)
numberFormat('123456')
numberFormat('123456.978', 2)
numberFormat('123456.9', 2, true)
数据反格式化
unNumberFormat(value)
unNumberFormat('123,456')