-14

i am trying to parse a float in javascript in this way:

var number = '25.492.381';

var cost = parseFloat(number);

This returns 25.492

I wish this would return 25.492.381

How can this be done?

  • 3
    Numbers don't work like that. If you really want `25.492.381`, just keep the string. – byxor Jun 22 '17 at 10:05
  • 1
    I've never seen a number have two decimal points – Lennholm Jun 22 '17 at 10:05
  • you want fractional part of the fractional part? – hmnzr Jun 22 '17 at 10:05
  • The `.` is the decimal point. You can not have a number with two decimal points. If you want to parse this, you have to remove the dots first (assuming those are actually meant to be thousand separators.) – CBroe Jun 22 '17 at 10:06
  • This shows how parseFloat works... https://www.w3schools.com/jsref/jsref_parsefloat.asp – selten98 Jun 22 '17 at 10:06
  • 1
    @all in some countrys, the normal number notation is 199.234.222,123 which is confusing for programmers the first time. – Jonas Wilms Jun 22 '17 at 10:08
  • In Europe, numbers are formatted like this: €1.234.567,89. See https://ux.stackexchange.com/questions/9105/international-currency-formatting-guidelines-currency-codes – david25272 Jul 04 '17 at 04:15
  • Does this answer your question? [JavaScript parseFloat in Different Cultures](https://stackoverflow.com/questions/12694455/javascript-parsefloat-in-different-cultures) – dloeda Oct 07 '20 at 07:55

2 Answers2

3

Remember that . in standard notation split the Integer and Fractional part of the number. You could want this:

var number = '25.492.381'.replace('.', '').replace(",",".") ;

var cost = parseFloat(number);
dloeda
  • 1,516
  • 15
  • 23
1

25.492.381 is not a float. You need to use a formatter. Check Numbro

Guillaume L.
  • 61
  • 1
  • 3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/16497697) – anoop Jun 22 '17 at 13:51
  • I understand but I don't know which format fit for him. – Guillaume L. Jun 23 '17 at 08:23