I want to convert string to float number but I have a problem
var b = parseFloat("10.525.142,25");
"output = "10.525"
var b = parseFloat("10.525,25");
"output = 10.525"
How I solution this?
I want to convert string to float number but I have a problem
var b = parseFloat("10.525.142,25");
"output = "10.525"
var b = parseFloat("10.525,25");
"output = 10.525"
How I solution this?
If parseFloat encounters a character other than a plus sign (+), minus sign (- U+002D HYPHEN-MINUS), numeral (0–9), decimal point (.), or exponent (e or E), it returns the value up to that character, ignoring the invalid character and characters following it.
parseFloat("10525142.25")
10525142.25
parseFloat("10525.25")
10525.25
To complete the previous answers and apply them to your problem. Remove dots from your string, then replace the comma with a dot.
// Your string values
var strA = "10.525.142,25";
var strB = "10.525,25";
function fromUSFormatToStandard(x) {
return x.replace(/\./g, "").replace(',', '.');
}
var a = parseFloat(fromUSFormatToStandard(strA)); // Output : 10525142.25 (float)
var b = parseFloat(fromUSFormatToStandard(strB)); // Output : 10525.25 (float)
Example : https://jsfiddle.net/jm3fr9d4/
The parseFloat() function parses a string and returns a floating point number.
This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.
Note: Only the first number in the string is returned,Leading and trailing spaces are allowed,If the first character cannot be converted to a number, parseFloat() returns NaN.
Content reference :
https://www.w3schools.com/jsref/jsref_parsefloat.asp
You can check this post for why its ignoring characters after comma.
Javascript parse float is ignoring the decimals after my comma