-3

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?

Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19
veliyetisgengil
  • 101
  • 2
  • 10
  • "*I have a problem*" – Can you be more specific? Showing code that does what it is supposed to do is not a problem. Would you expect 10525142.25 instead? – str Sep 30 '19 at 08:16
  • Remove all the thousand separators using `.replace(/\./g, "")` and then replace the decimal separator with a `.` using `.replace(",", ".")` – nick zoum Sep 30 '19 at 08:20

3 Answers3

0

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

parseFloat specification

OMANSAK
  • 1,066
  • 1
  • 12
  • 30
0

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/

Jeremy-F
  • 93
  • 6
  • it show 10525142.25 (float) but I want to show 10.525.142,25 – veliyetisgengil Sep 30 '19 at 10:18
  • I solve your first problem about converting string to float. You can now use "toLocalString" function and force the location. Example : `x.toLocaleString("de-DE")` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString – Jeremy-F Sep 30 '19 at 11:28
-2

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

redhatvicky
  • 1,912
  • 9
  • 8
  • Answer copied from [here](https://discuss.codecademy.com/t/error-make-sure-your-if-else-if-else-statement-returns-a-string-when-the-function-input-isnt-a-number/74079/4) without giving any attribution. – str Sep 30 '19 at 08:20
  • There is a difference between the answer and the help content in the above section , The answer has been linked as a reference , while the help content was refered from W3 , Not from any other answer as you have mentioned . Any way i have updated the reference of the content also. – redhatvicky Sep 30 '19 at 09:06
  • 1
    It does not matter where you copied it from. *Always* provide the source. – str Sep 30 '19 at 09:12