Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 12x 12x | import { round } from 'lodash-uni'
/**
* 保留 n 位小数下的 x 舍 y 入。
*
* @param number 数值
* @param precision 精度
* @param threshold 舍入阈值,等于大于这个值时入,小于这个值时舍
*/
export function roundTo(number: number, precision = 0, threshold = 5): number {
const [int, decimal] = number.toFixed(precision + 2).split('.')
return round(
+`${int}.${decimal.slice(0, precision)}${
+decimal[precision] >= threshold ? '9' : '0'
}`,
precision,
)
}
|