0

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

4
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Troy III
0
<html>
<head> </head>
<script lang='javascript'>
var amount1=12345678.10;
var amount2=87246125.00;
var d=amount1.valueOf()+amount2.valueOf();
alert(d);
</script>

</html>

Edited by urtrivedi: n/a

0

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

0
<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>

Edited by urtrivedi: n/a

0

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

0

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

0

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

0
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;

Edited by Troy III: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.