简体   繁体   中英

How to sum numbers that are in currency format using jquery

I am trying to sum up the following numbers.

 var number1= 12,000.00 ;  var number2= 12,000.00;

I have tried this alert(number1+number2);​ but it doesn't return any data.

Could you please help me to solve this problem?

Thanks

The code in your question is invalid javascript. You can't have a , inside a numeric literal. You have to store it as a string, then parse it manually:

var number1 = '12,000.00';
var number2 = '12,000.00';

function parseCurrency( num ) {
    return parseFloat( num.replace( /,/g, '') );
}

alert( parseCurrency(number1) + parseCurrency(number2) );

This won't work. Use accounting.js 's unformat function to parse 12,000 as a string instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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