gpt4 book ai didi

JavaScript如何显示数字

转载 作者:行者123 更新时间:2023-12-02 20:00:59 29 4
gpt4 key购买 nike

我在 JavaScript 函数中计算了一个数字。

示例:

var price = 1000;
var commission = 200;

var total = price + commission

添加这两个之后,我想将总数显示为 1,200(将 , 放在 3 位数字之间)。

最佳答案

来自:http://www.mredkj.com/javascript/numberFormat.html

This functionality is not built into JavaScript, so custom code needs to be used. The following is one way of adding commas to a number, and returning a string.

function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}

关于JavaScript如何显示数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008376/

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