Dear Coders

var amount1=12,345,678.10
var amount1=87,246,125.00

I want to sum amount1+amount2 as 99,591,803.10

Please help

<html>
<head> </head>
<script lang='javascript'>
var amount1=12345678.10;
var amount2=87246125.00;
var d=amount1.valueOf()+amount2.valueOf();
alert(d);
</script>

</html>

Sir my variable were as

var amount1=12,345,678.10
var amount1=87,246,125.00

Please do not remove comma separator and help again

<html>
<head> </head>
<script lang='javascript'>
var amount1=" 12,345,678.10 ";
var amount2="87,246,125.00";

re = /,/gi;
var d=parseFloat(amount1.replace(re, ""))
       +parseFloat(amount2.replace(re, ""));

alert(d);
</script>

</html>

Dear Sir, the Problem is 80% Solved
alert shows 99591803.1
but ...
I need 99,591,803.10
Please add comma separator and decimal point in result
Thanks

I think there is no way to format number in java script to thousand separator, you need to create your own function.

On insertion of comma-separators numbers become strings, therefore for display purposes only.

If further arithmetic operations are necessary, keep an unformatted, true Number representation of the original value in javascript.

Approached the right way, you should never need to convert comma-separated number-strings back to true Numbers.

Airshow

var amount1='12,345,678.10';
var amount2='87,246,125.00';
/* ----------------8<---------------------- */
add=
/*b.b. Troy III p.a.e.*/
function(a,b,s,z){
      a=a.replace(/,/g,'').split('.');
      b=b.replace(/,/g,'').split('.');
//avoiding js floats 
     s=Number(a[0]) + Number(b[0]);
     s+= ((Number(a[1]) + Number(b[1])) / 100).
     toPrecision(2);

z = new String((''+s)./*
	credit: Jeffrey Friedl
	*/replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));

z.numericValue = new Number(s);
return z
}
/* ------------------->8------------------- */
console.log(
      add(amount1,amount2)
)
console.log(
      add(amount1,amount2).numericValue
)
// -------------------------------------- 
//when done using it
delete add;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.