Guest, before posting your code please take these rules into consideration:
It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
Don't share a wall of code. All we want is the problem area, the code related to your issue.
Have you checked the Console log output ? Your code produces errors on all 4 lines. Two things are wrong:
JavaScript does not recognize 123,45 and 9,22 as valid numbers.
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;
alert(c.toLocaleString("de-DE"));
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
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:
JavaScript does not recognize 123,45 and 9,22 as valid numbers.
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;
alert(c.toLocaleString("de-DE"));
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.
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.
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
alert(q);
var c = q.replace(".", ","); //convert back to 0,00 format
alert(c);
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.