Javascript 添加数量并放入元素
我想把两个量加在一起,每个量来自不同的元素,然后把它们放在另一个元素中。棘手的部分是将“$”符号和逗号(,)保留在总金额中(或者以后将它们添加到总金额中?) 有人知道Javascript可以做到这一点吗Javascript 添加数量并放入元素,javascript,Javascript,我想把两个量加在一起,每个量来自不同的元素,然后把它们放在另一个元素中。棘手的部分是将“$”符号和逗号(,)保留在总金额中(或者以后将它们添加到总金额中?) 有人知道Javascript可以做到这一点吗 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.o
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add amounts and put in element</title>
</head>
<body>
<p id="firstAmount">$1,133.79</p>
<p id="secondAmount">$1,900.00</p>
<br />
Total: <p id="totalAmount">$0.00</p>
</body>
</html>
作者:
这解决了小数点后零消失的问题。例如,如果您有$1133.00+$1900.00,您将看到$3033
使用此修复程序,您将看到小数点=>3033.00美元后的零 你可以看到这个活生生的例子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add amounts and put in element</title>
</head>
<body>
<p id="firstAmount">$1,133.79</p>
<p id="secondAmount">$1,900.00</p>
<br />
Total: <p id="totalAmount">$0.00</p>
</body>
</html>
********************更新1***********************
您可以找到工作代码的一个实例,以下代码应该可以工作,但没有测试它:
function addCommasandsign(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;
}
//get html
var firstval=document.getElementById('firstAmount').innerHTML;
var secval=document.getElementById('secondAmount').innerHTML;
//replace $ and , by nothing
firstval=firstval.replace('$','');
firstval=firstval.replace(',','');
secval=firstval.replace('$','');
secval=firstval.replace(',','');
//add values
var total=firstval + secval;
//put , and $ back in place
var total2=addCommasandsign(total);
//insert in html
document.getElementById('totalAmount').innerHTML=total2;
要将“$1133.79”转换为实数,可以执行以下操作:
var tmp = '$1,133.79'.replace(/^\$/,'').split(/[,.]/),
value = tmp.length === 3
? parseFloat(tmp.slice(0,2).join('')+'.'+tmp.slice(tmp.length-1))
: parseFloat(tmp.join(''));
或者使用以下命令:
var Currency = function(){
function tryParse(val){
var tmp = val.match(/\d+[.,]\d+[.,]\d+|\d+[.,]\d+/g)[0].split(/[,.]/);
if (tmp.length < 2 || tmp.length>3){
throw 'invalid input value';
}
return tmp.length === 3
? Number(tmp.slice(0,2).join('')+'.'+tmp.slice(tmp.length-1))
: Number(tmp.join(''));
}
return {parse: tryParse};
}();
//usage
Currency.parse('€ 30.000,23'); //=> 30000.23
Currency.parse('$30,000.23'); //=> 30000.23
Currency.parse('Pound Sterling £30,000.23'); //=> 30000.23
Currency.parse('30,000.23 (dollars)'); //=> 30000.23
Currency.parse('₯30.000,23'); //=> 30000.23
//etc.
var Currency=function(){
功能性胰蛋白酶(val){
var tmp=val.match(/\d+[,]\d+[,]\d+[,]\d+/g)[0]。拆分(/[,.]/);
如果(tmp长度<2 | | tmp长度>3){
抛出“无效输入值”;
}
返回tmp.length==3
?编号(tmp切片(0,2).连接(“”)+.+tmp切片(tmp长度-1))
:编号(tmp.连接(“”));
}
返回{parse:tryParse};
}();
//用法
货币。解析('30000欧元,23');//=>30000.23
Currency.parse('30000.23');//=>30000.23
货币解析('30000.23英镑');//=>30000.23
Currency.parse('30000.23美元);//=>30000.23
Currency.parse('₯30.000,23');/>30000.23
//等等。
您可以尝试以下方法():
函数添加(x1,x2){
//例如x1=“$1133.79”,x2=“$1900.00”
x1=x1。替换(/[$,]/g,”);
x2=x2。替换(/[$,]/g,”);
var和=(parseFloat(x1)+parseFloat(x2)).toFixed(2);
var部分=总分割数(“.”);
var len=零件[0]。长度;
sum=“$”;
对于(变量i1=0,i2=len%3;I21)和+=“”+部分[1];
回报金额;
}
var总和=加上(“$1133.79”,“$1900.00”);//="$3,033.79"
对不起,我忘了一个零件。。。在添加值之前,您需要去掉、和$符号。。。您可以使用string.replace()执行此操作,并将其替换为nothing“谢谢@Kasper,看起来很棒!不过,在计算中似乎有一个小问题。我得到的总金额是1133.791133美元,而不是3033.79美元。请参见此实时示例:。你知道问题出在哪里吗?现在代码正常了再次感谢你的帮助@kasper taeymanst代码正常了,请看下面的一个实例:
var Currency = function(){
function tryParse(val){
var tmp = val.match(/\d+[.,]\d+[.,]\d+|\d+[.,]\d+/g)[0].split(/[,.]/);
if (tmp.length < 2 || tmp.length>3){
throw 'invalid input value';
}
return tmp.length === 3
? Number(tmp.slice(0,2).join('')+'.'+tmp.slice(tmp.length-1))
: Number(tmp.join(''));
}
return {parse: tryParse};
}();
//usage
Currency.parse('€ 30.000,23'); //=> 30000.23
Currency.parse('$30,000.23'); //=> 30000.23
Currency.parse('Pound Sterling £30,000.23'); //=> 30000.23
Currency.parse('30,000.23 (dollars)'); //=> 30000.23
Currency.parse('₯30.000,23'); //=> 30000.23
//etc.
function add(x1, x2) {
// e.g. x1 = "$1,133.79", x2 = "$1,900.00"
x1 = x1.replace(/[$,]/g, "");
x2 = x2.replace(/[$,]/g, "");
var sum = (parseFloat(x1) + parseFloat(x2)).toFixed(2);
var parts = sum.split(".");
var len = parts[0].length;
sum = "$";
for (var i1 = 0, i2 = len % 3; i2 <= len; i1 = i2, i2 += 3) {
sum += parts[0].slice(i1, i2);
if (i2 < len) sum += ",";
}
if (parts.length > 1) sum += "." + parts[1];
return sum;
}
var sum = add("$1,133.79", "$1,900.00"); // == "$3,033.79"