Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用句点作为数字中的千位分隔符_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用句点作为数字中的千位分隔符

Javascript 使用句点作为数字中的千位分隔符,javascript,jquery,html,Javascript,Jquery,Html,因此,我有一个数字var test\u price=94600。我想将它乘以用户在元素中键入的值,并在元素中显示该结果。这是自描述的:在元素中,它显示了总价(价格*数量)。我已经做到了: $('input[name="quantity"]').change(function () { var test_price = 94600; var quantity = $('input[name="quantity"]').val(); var price_without_tho

因此,我有一个数字
var test\u price=94600
。我想将它乘以用户在
元素中键入的值,并在

元素中显示该结果。这是自描述的:在
元素中,它显示了总价(
价格*数量
)。我已经做到了:

$('input[name="quantity"]').change(function () {
    var test_price = 94600;
    var quantity = $('input[name="quantity"]').val();
    var price_without_thousands_separator = $('#sample').html(test_price * quantity);
});

现在我只想在
#sample
段落中每隔三个单位添加一个句点(.)。因此,如果数量为1,则显示为94.600,如果数量为3,则显示为283.800,如果数量为8,则显示为756.800,依此类推。是的,我知道通常的传统是使用逗号,但在我的国家,我们习惯于将句点读作千位分隔符,将逗号读作小数(例如,对于我们π=3.14,3,14)


节日快乐

这是一个非常简单的解决方案,最多可容纳6位数字

var x = test_price * quantity;
x = x.toString();
var withComma = x.slice(0, x.length - 3) + ',' + x.slice(-3);

为了使其更加健壮,您可能需要创建一个formatInt()函数,该函数在字符串中循环,并在从末尾开始的每三个数字处添加一个逗号。上面是重复的,因为您只需在它给出的代码中切换
,“
。我如何插入他的代码(
函数编号,带逗号(x)){return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g,“,”;}
)到我的中?我不理解你必须添加这个函数,并在设置html变量price_时调用它,而不使用$('#sample').html(带逗号的数字(test price*quantity));Hey@bormat非常感谢!有些事情我还不太了解,比如JS中的参数/参数。如果有人想测试新代码,我该如何使用
formatInt()
?,但一旦输入值大于或等于11,数字将显示为1040.600而不是1.400.600。我将使用此正则表达式解决方案。因此,在您的情况下,添加该函数并使用逗号调用numberWithCommas(test_price*quantity)。此外,将“,”替换为“.”