0

I have

 var value = $120,90
 var value = $1,209.00

currently I replace the first case with

 value = value.replaceAll(",", ".").replaceAll("[^0-9.]*", "");

which gives me that I am looking for: the integer 12090

with the second case I run in a problem however like this. How can I solve this in Javascript?

bobbel
  • 3,327
  • 2
  • 26
  • 43
user2843661
  • 19
  • 1
  • 5

3 Answers3

0

I cannot see how you can make an algorithm work unless you insist that everyone enters dollars and cents. The only option I can think of is to use locale to determine the number separator.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • Tom: I was thinking if its possible to count the numbers on the left side after the coma and in case > 2 then we remove coma. – user2843661 Jan 30 '14 at 11:44
0

Could you use the answer from this thread?

How can I remove the decimal part from JavaScript number?

They use Math.floor() (round down), Math.ceil() (round up) or Math.round() (round to nearest integer).

Community
  • 1
  • 1
Avisari
  • 101
  • 1
  • 4
0

You may modify you regexp.

value  = value.replace(/,/g, ".").replace(/^\D|\.(?!\d*$)/g, "");

First will replace ',' to '.' and the 2nd replace NON-digit symbols in the beginning of the string and all dots EXCEPT the last one with the empty string. Then use parseFloat.

To be sure completely it's better to create a template for data input and don't allow users to enter values in an invalid format.

Kiril
  • 2,935
  • 1
  • 33
  • 41