简体   繁体   English

Javascript货币格式

[英]Javascript Currency Format

Hello all I am in need of some help here. 您好,我在这里需要一些帮助。 I am in no way a javascript programmer but I have pieced together some code to do what I need it to do, but I am having trouble with the currency formatting of what I have. 我绝对不是JavaScript程序员,但我拼凑了一些代码来完成我需要做的事情,但是我在使用货币格式方面遇到了麻烦。 In the code below myForm.man_quan.value doesn't need the $ but does need commas separating the thousands, and myForm.man_price.value needs the $, commas, and .00 if the price is without cents. 在下面的代码中,myForm.man_quan.value不需要$,但需要用逗号分隔千位,而myForm.man_price.value需要$,逗号和.00(如果价格不含美分)。

Basically I need to be able to add the $ and , and .00 to what I have. 基本上,我需要能够将$和.00添加到我所拥有的内容中。

function runTotal(myForm) {
var form_field1 = Number(myForm.man_quan.value);
var form_field2 = Number(myForm.man_price.value);
myForm.man_total.value = form_field1.toFixed(0) * form_field2.toFixed(2);
}

Try 尝试

function runTotal(myForm) {
var form_field1 = formatmoney(myForm.man_quan.value);
var form_field2 = formatmoney(myForm.man_price.value);
myForm.man_total.value = form_field1.toFixed(0) * form_field2.toFixed(2);
}  

Replace or remove unnecessary symbol like $ and comma 替换或删除不必要的符号,例如$和逗号

form_field1 = myForm.man_quan.value.replace(',', '.');
form_field2 = myForm.man_price.value.replace('$', '');
myForm.man_total.value = form_field1 * form_field2;

i think your looking for parseInt() http://www.w3schools.com/jsref/jsref_parseint.asp since it's currency, you might want to use parseFloat http://www.w3schools.com/jsref/jsref_parsefloat.asp 我认为您正在寻找parseInt() http://www.w3schools.com/jsref/jsref_parseint.asp,因为它是货币,因此您可能想使用parseFloat http://www.w3schools.com/jsref/jsref_parsefloat.asp

so it sould be somthing like: 所以它就像这样:

function runTotal(myForm) {
    var form_field1 = parseFloat(myForm.man_quan.value);
    var form_field2 = parseFloat(myForm.man_price.value);
    myForm.man_total.value = form_field1.toFixed(0) * form_field2.toFixed(2);
}

Re-adding the , for the thousands, might be more tricky. 为成千上万个重新添加,可能会比较棘手。 but you can reform the var to string with toString() and adding the $ sign like so. 但是您可以使用toString()将var修改为字符串,并像这样添加$符号。

var output = "$"+myForm.man_total.value.toString();

very simple function in JavaScript called toLocaleString JavaScript中非常简单的函数,称为toLocaleString

Note: Data type should be integer 注意:数据类型应为整数

function numTocurrency(num)
{
   return parseInt(num).toLocaleString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM