3

I have this string containing a number with a comma that is like this "9,848.48". I want to convert it to its floating equivalent which is 9848.48. I tried using parseFloat() but the result I got is 9. How can this be done in javascript?

user229044
  • 232,980
  • 40
  • 330
  • 338
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • 1
    *"PS: please do not downvote as I am at risk of being blocked..."* That sort of thing is counter-productive. If you've asked so many questions that have been downvoted, **your** behavior needs to change, not the behavior of the community. In this case, for instance, just read the documentation of the `parseFloat` fucntion, which is perfectly clear. If you don't like [the spec](http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.3) (it **is** really hard to read), there's [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). – T.J. Crowder Dec 04 '14 at 09:02
  • And/or search: http://stackoverflow.com/questions/3205730/javascript-parsefloat-500-000-returns-500-when-i-need-500000 – T.J. Crowder Dec 04 '14 at 09:03
  • Certainly not suggesting to change the behavior of the community. I believe the downvotes are my fault. Unfortunately, some members of the community downvote without telling me why even if the reason is legitimate. I cannot change my behavior if I do not know why. Not everyone is as kind as you, Sir. – guagay_wk Dec 04 '14 at 09:10
  • The [help] has quite a large amount of guidance on the subject. – T.J. Crowder Dec 04 '14 at 09:11

6 Answers6

4

Remove the , and convert it to float using parseFloat

parseFloat("9,848.48".replace(',', ''));
// 9848.48
Weafs.py
  • 22,731
  • 9
  • 56
  • 78
1

You could remove all commas from the string before calling parseFloat:

val = val.replace(',', '');
var parsed = parseFloat(val);
DoctorMick
  • 6,703
  • 28
  • 26
1

var number="9,848.48"; number=number.replace(/\,/g,''); number=parseInt(number); remove the comma then parse to int

yk1007
  • 151
  • 2
  • 16
1

You just need to remove commas from the string, try this:

parseFloat('9,848.48'.replace(',', ''));
Ken Stipek
  • 1,512
  • 8
  • 12
1

You can use the replace method in javascript to replace "," with "".

After that you do your necessary parsing.

Srinivas B
  • 1,821
  • 4
  • 17
  • 35
-1

Remove the comma before calling parseFloat

HBP
  • 15,685
  • 6
  • 28
  • 34