我使用NumeralJS格式化金额,如下所示;
numeral(unformattedValue).format(amtFormat);因此,如果amtFormat设置为"0,0.00",那么如果金额输入为"123.1",则得到的结果为"123.10“。
但我想要的是没有格式/四舍五入等发生在小数点后…因此,如果输入值是1.999999,我希望输出是1.999999 (小数点后没有任何变化)
有没有可能向format()传递一些东西来实现这一点?我在NumeralJS文档中找不到任何东西。
发布于 2020-06-19 12:57:34
鉴于您对以下方面的要求:
下面是一个你可以尝试的选项。可能有一种更简单的方法可以做到这一点,也许是通过定义一个自定义的format,但这里有一个想法:
// example input
let input = [1549.9988, 1549123.9912938, 123.456];
input.forEach(i => {
// count the number of decimals
let numberOfDecimals = i.toString().split(".")[1].length;
// construct a string representing the number of decimals to retain
let decFormat = "0".repeat(numberOfDecimals - 1);
// format the number using this string
let num = numeral(i).format("0,0[." + decFormat + "]");
console.log(num);
});<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
https://stackoverflow.com/questions/62463173
复制相似问题