gpt4 book ai didi

javascript - 使用 .toFixed() 仅在需要时显示小数点后 3 位

转载 作者:行者123 更新时间:2023-11-30 09:21:23 24 4
gpt4 key购买 nike

我如何使用 .toFixed() 来得到这个结果。

number     display
------ -------
1 1
1.23 1.23
1.23456 1.235

我一直在使用 .toFixed(3)。但这是展示。

number     display
------ -------
1 1.000
1.23 1.230
1.23456 1.235

最佳答案

你可以尝试使用:

Math.round(yournum * 1000) / 1000

或者你可以试试:

+(Math.round(num + "e+3")  + "e-3")

关于javascript - 使用 .toFixed() 仅在需要时显示小数点后 3 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51418434/

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