gpt4 book ai didi

javascript - 在 Javascript 中格式化为货币

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:14 25 4
gpt4 key购买 nike

我有一个变量,它由另外 2 个变量的乘积组成

 var EstimatedTotal =  GetNumeric(ServiceLevel) * GetNumeric(EstimatedCoreHours);

这是否可能,如果可能的话如何,或者将其格式化为货币所调用的函数是什么?我一直在谷歌搜索,只是我找不到一个函数,我所看到的唯一方法是真的,真的很啰嗦

最佳答案

取自此处:http://javascript.internet.com/forms/currency-format.html .我用过它,效果很好。

function formatCurrency(num)
{
num = num.toString().replace(/\$|\,/g, '');
if (isNaN(num))
{
num = "0";
}

sign = (num == (num = Math.abs(num)));
num = Math.floor(num * 100 + 0.50000000001);
cents = num % 100;
num = Math.floor(num / 100).toString();

if (cents < 10)
{
cents = "0" + cents;
}
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
{
num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
}

return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

关于javascript - 在 Javascript 中格式化为货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6124644/

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