0

I am trying to figure out how to convert an amount to an integer in Javascript. E.g.

Input 10.555,95 Output 1055595 Input 9.234,77 Output 923477

I was thinking about removing the dot and the comma, but I don't know if that would be efficient

App
  • 159
  • 2
  • 2
  • 6

3 Answers3

1

Based on your use cases, you want to make an integer by replacing the special chars in a string. You need to replace like this

 parseInt("9.234,77".replace(",","").replace(".",""))
Thanga
  • 7,811
  • 3
  • 19
  • 38
1

Since you wish to remove everything (regardless of formatting), you could replace any non-numeric characters and then force their type:

var intValue = + '10.555,95'.replace(/[^\d]/g,''); // == 1055595;
BenM
  • 52,573
  • 26
  • 113
  • 168
0
parseFloat('10.555,95'.replace('.','').replace(',',''));

inspired by this answer: JavaScript: Parse a string to a number?

Community
  • 1
  • 1
xszaboj
  • 1,035
  • 7
  • 21
  • The output if this code doesn't fit the requirements – Jan Feb 13 '17 at 13:39
  • It's been edited. It used to substitute comma for dot and then parse it as a float. – Jan Feb 13 '17 at 13:42
  • Yeah, sorry I've noticed that it should be always integer after I've submitted the answer. So you could probably use parseInt as well – xszaboj Feb 13 '17 at 13:47