添加金额并放入元素

时间:2011-09-15 09:51:59

标签: 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.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>



** * ** * ** * 的** * ** * ** * ** 更新2 ** * ** * ** * ** * ** * ** * ****

替换:

var total2=addCommasandsign(total);

由:

var total2=addCommasandsign(total.toFixed(2));


这解决了小数点后零点消失的问题。例如,如果您有$ 1,133.00 + $ 1,900.00,您将看到$ 3,033
使用此修复程序,您将看到小数点后的零=&gt; $ 3,033.00

您可以看到实时示例here


** * ** * ** * 的** * ** * ** * ** 更新1 ** * ** * ** * ** * ** * ** * ****

您可以找到工作代码here

的实际示例

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

要将'$ 1,133.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.

答案 2 :(得分:0)

您可以尝试此操作(jsFiddle):

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"