For example, you receive JSON data from server and all decimal numbers are received with "," separator because of server/user settings or programming language.
when i call the following js function:
function calculate() {
var x = '2,5';
var y = '2.5';
var doubleX = parseFloat(x) * 2;
var doubleY = parseFloat(y) * 2;
alert('double x : ' + doubleX + '\n' + 'double y : ' + doubleY);
}
i see following results in the alert box
double x : 4
double y : 5
Here is the tricky function that i created to handle this problem :
function FixNumberDecimalSeparator(value) {
return value.replace(',', '.');
}
When i use this function in the calculate function :
var x = '2,5';
var y = '2.5';
x = FixNumberDecimalSeparator(x);
y = FixNumberDecimalSeparator(y);
var doubleX = parseFloat(x) * 2;
var doubleY = parseFloat(y) * 2;
alert('double x : ' + doubleX + '\n' + 'double y : ' + doubleY);
i see following results in the alert box
double x : 5
double y : 5
I hope this function will be useful for you.
0 comments:
Post a Comment