gpt4 book ai didi

javascript - Number.toLocaleString(),minimumFractionDigits 的作用类似于 max

转载 作者:行者123 更新时间:2023-12-03 02:53:21 28 4
gpt4 key购买 nike

我有这个代码:

10.00000001.toLocaleString('en-GB', {useGrouping: true, minimumFractionDigits: 2})

我希望它返回'10.00000001',但我得到的是10.00。当我更改最小值时,准确度也会相应变化。

最小值就像最大值一样。

设置maximumFractionDigits不会改变任何东西。它被完全忽略。

我使用节点 8.1.4 和 FF Quantum 对此进行了测试。

知道为什么 toLocaleString 表现得如此奇怪吗?

最佳答案

根据文档https://www.jsman.net/manual/Standard-Global-Objects/Number/toLocaleString ,最小值是您想要的小数位数。下面两个例子可以让大家清楚地理解

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);

关于javascript - Number.toLocaleString(),minimumFractionDigits 的作用类似于 max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47742977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com