gpt4 book ai didi

javascript - 限制javascript中小数点后显示的数字数量

转载 作者:行者123 更新时间:2023-12-02 23:02:39 25 4
gpt4 key购买 nike

嘿,我有一些像这样的花车

4.3455
2.768
3.67

我想像这样显示它们

4.34
2.76
3.67

我不想对数字进行四舍五入,只需将小数点后显示的数字数量限制为 2。

最佳答案

您正在寻找toFixed :

var x = 4.3455;
alert(x.toFixed(2)); // alerts 4.35 -- not what you wanted!

...但看起来您想要 chop 而不是舍入,所以:

var x = 4.3455;
x = Math.floor(x * 100) / 100;
alert(x.toFixed(2)); // alerts 4.34

关于javascript - 限制javascript中小数点后显示的数字数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4256030/

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