我有这段代码:
10.00000001.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 2})
我希望它返回'10.00000001'
,但我得到的是10.00
。
当我改变最低限度时,相应性会相应变化。
最小值就像是最大值。
设置maximumFractionDigits
不会改变任何内容。它完全被忽略了。
我用节点8.1.4和FF Quantum测试了这个。
为什么toLocaleString
行为如此奇怪?
答案 0 :(得分:1)
根据文档https://www.jsman.net/manual/Standard-Global-Objects/Number/toLocaleString,minimum是十进制所需的位数。 以下两个例子将给出清晰的理解
var n = 10.00000001;
var x;
// It will give 8 decimal point because min is 0 (i.e. Atleast it should have one decimal point) and max it can have till 8
x = n.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 0, maximumFractionDigits: n.toString().split('.')[1].length});
console.log(x);
// If you put value 2.301 it gives 2.3 since it omits 0 in 2.30 (i.e.
n = 2.311;
// It will give 1 decimal point because min to max is 1
x = n.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 1, maximumFractionDigits: 2});
console.log(x);
// It will give 1 decimal point eventhough we didn't have decimal points
n = 2;
x = n.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 1});
console.log(x);