gpt4 book ai didi

javascript - 向字符串添加小数

转载 作者:行者123 更新时间:2023-11-29 15:49:41 25 4
gpt4 key购买 nike

我有一个脚本,它需要一个大数字并进行计数。该脚本将数字转换为字符串,以便可以使用逗号进行格式化,但我还需要在最后两位数字前添加一个小数位。我知道这一行处理逗号:

if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;

我可以添加类似的代码行来完成添加小数点吗?

最佳答案

是的,如果你总是想要最后两位之前的小数:

function numberIt(str) {
//number before the decimal point
num = str.substring(0,str.length-3);
//number after the decimal point
dec = str.substring(str.length-2,str.length-1)
//connect both parts while comma-ing the first half
output = commaFunc(num) + "." + dec;

return output;
}

commaFunc() 是您描述的添加逗号的函数时。

编辑

经过一番努力,完整正确的代码:

http://jsfiddle.net/nayish/TT8BH/21/

关于javascript - 向字符串添加小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7150575/

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