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) //"123.68"
round('123.657', 1, RoundingModes.DOWN) // "123.6"
round('123.657', 2, RoundingModes.CEILING) // "123.66"
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) // "12"
floor(-12.3) // "-13"
ceil
ceil(number) Returns the whole number nearest but not lesser than the input number.
ceil(12.8) // "13"
ceil(-12.3) // "-12"
negate
Returns negation of a given number.
negate('123.678') // "-123.678"
negate('-1234') // "1234"
对比
numCompareTo(number1, number2) 比较两个数据,Returns 1, 0 and -1 if number1 > number2, number1 == number2 and number1 < number2 respectively.
compareTo('23.678', '67.34') // value = -1
compareTo('23.678', '23.6780') // value = 0
compareTo('123.678', '67.34') // value = 1
判断数字是否在范围内
inScope(number, min, max)
inScope('1', '1', '5') // false
inScope('3', '1', '10') // true
对数字进行扩展
scaleNumber(value, scale) 如果 scale>0 代表 multiply(value,scale) 如果 scale<0 代表 divide(value, negate(scale))
scaleNumber('100', '5') // 500
scaleNumber('100', '-5') // 20
数据格式化
numberFormat(value, decimalLength = -1, autoPadding = false, digits = 3, separator = ',')
- value 待格式化 value
- decimalLength(小数长度)
- autoPadding(是否自动填充 0)
- digits(分块长度)
- separator(分块隔离符号)
numberFormat('123456') // 123,456
numberFormat('123456.978', 2) // 123,456.97
numberFormat('123456.9', 2, true) // 123,456.90
数据反格式化
unNumberFormat(value)
unNumberFormat('123,456') // 123456