# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# JavaScriptJavascript Math International Number format

#### Lee Bornstein

##### New Coder
How do I add two decimal numbers when number are 999,99 (german format)

Var a=123,45;
Var b=9,22;
Var c=a+b;

would like to get sum 132,67

sample code would be greatly appreciated

Have you checked the Console log output ? Your code produces errors on all 4 lines. Two things are wrong:
1. JavaScript does not recognize 123,45 and 9,22 as valid numbers.
2. You have to use `var`, not `Var`. JavaScript is case sensitive.
Now how to get JS to accept the German decimal format ? I don't think it is possible. While there is a function `toLocaleString()` which can create localized output, there doesn't seem to be a function `fromLocaleString()` to accept localized input (123,34) and turn it into a JS decimal value. So in your code, I think you have to use the decimal point. Then if you want German output, you can do this:

JavaScript:
``````var a=123.45;
var b=9.22;
var c=a+b;

which will output 132,67

A couple of problems here Lee. First is the capitalization of var to Var. Use the lower case. Second, the math library in JS uses decimals and not commas. We could write a function to change the commas to dots and then back again if you want.
Else
Code:
``````var a=123.45;
var b=9.22;
var c = a+b;
And you have to limit the answer to 2 decimals.

Haha cbreemer got here first with same answer.

Haha cbreemer got here first with same answer.
Haha got you back there 😁 It's the old guys like us who are quickest.

And you have to limit the answer to 2 decimals.
Interestingly, that is true in Node.js, which otherwise reports something silly like `132.670000002`. But in a browser like Chrome that doesn't seem to be necessary, you get `123.67` as expected. Hmmmm....

Have you checked the Console log output ? Your code produces errors on all 4 lines. Two things are wrong:
1. JavaScript does not recognize 123,45 and 9,22 as valid numbers.
2. You have to use `var`, not `Var`. JavaScript is case sensitive.
Now how to get JS to accept the German decimal format ? I don't think it is possible. While there is a function `toLocaleString()` which can create localized output, there doesn't seem to be a function `fromLocaleString()` to accept localized input (123,34) and turn it into a JS decimal value. So in your code, I think you have to use the decimal point. Then if you want German output, you can do this:

JavaScript:
``````var a=123.45;
var b=9.22;
var c=a+b;

which will output 132,67

Thanks for the response..

So, that being the case - input number could be 123,45 or 123.45 depending on client Culture.
I do know the client Culture de-DE or en-US etc based upon his login so need only to convert comma to decimal and back again if it's a comma not decimal point.

Any easy function to do that?

Thanks for the response..

So, that being the case - input number could be 123,45 or 123.45 depending on client Culture.
I do know the client Culture de-DE or en-US etc based upon his login so need only to convert comma to decimal and back again if it's a comma not decimal point.

Any easy function to do that?
Yes. The replace() function.

Was able to replace commas in my two variables with periods so I could do var q = b - a; and get desired results.
When I then attempt to convert value of 11.2 back to 11,2 the replace command var c = q.replace(".", ","); fails and script aborts

I get to the alert(q); with desired value then next instruction fails and I don't get any further.

Here's my code... what am I doing wrong?
I'm stumped!

Code:
``````<script type="text/javascript">
function TestNumeric() {
var work = document.getElementById("txtOnHand").value;  //Get 1st value 0,00
var a = work.replace(",", ".");                         //convert to 0.00 format

var test = document.getElementById("txtCount").value;   //Get 2nd value 0,00
var b = test.replace(",", ".");                         //convert to 0.00 format

var q = b - a;                                          //Calc difference as 0.00
var c = q.replace(".", ",");                          //convert back to 0,00 format
document.getElementById("txtChange").value = c.toFixed(2);  //Return value to textbox

}``````

You have two strings a and b. Then you say `var q = b - a;`. Both b and a happen to be numeric strings, so JS silently converts them to numeric before doing the subtraction, and you now have a numeric variable q ! Then you try to do a string replace on q which obviously fails. Whereas JS converts string to numeric whenever needed, the converse is not true. If you want to do string stuff with q, you must make sure it is a string and not a numeric. If you replace `var q = b - a;` by `var q = "" + (b - a);` I think it will work. because now you create a string type variable.

Thanks - that worked.

Another solution I found is q = (b-a).tostring;

Last problem...
14,000000000000004 = 35,2 - 21,2
how do I get this trimmed to two decimal places like 14,00

Something has to be done here I would think.....

document.getElementById("txtChange").value = c;

Yes, that is the better solution for making a number into a string.
As for trimming to fixed decimals:
JavaScript:
``````var c = 14.000000000000004;
document.getElementById("txtChange").value = c.toFixed(2);``````
For this, you need the variable to be numeric. So do this first before changing to comma notation.