0

I have a text box whose outcome is in Belgium number format like this

<input type="text" name="valeurCatalogue" kendo-numeric-text-box="" k-culture='"fr-BE"' k-spinners="{{false}}" ng-disabled="isValueCatalogDisabled" ng-model="tarifForm.Vehicle.valeurCatalogue" />

Having put the value as 1525,8 the value gets transformed to 1.525,80 which is correct.

Now if I apply Number(1525,8) I get NAN.How to get the number in javascript?

Please note in debug mode I see the value 1525,8 as string.

Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
  • 1
    Not to remove but replace with dot: Number("1525,8".replace(/,/g, '.')) – Vladimir G. Jul 12 '16 at 07:33
  • Use `parseFloat("1525,8")` : convert string into float. – Gaetan Jul 12 '16 at 07:39
  • Possible duplicate of [Convert String with Dot or Comma as decimal separator to number in JavaScript](http://stackoverflow.com/questions/7431833/convert-string-with-dot-or-comma-as-decimal-separator-to-number-in-javascript) – Liam Jul 12 '16 at 07:40

1 Answers1

6

The problem here is that javascript uses the american way of expressing numbers (as do most programming languages I've encountered). So, 1,5 is not one and a half as you would expect, rather it's not a valid number. Thus when you try to parse it you get NaN (Not a Number). In javascript, the correct way to write said number would be 1.5. If you have 1525,8 the simple way to do this is replace all commas with dots like this:

const numStr = '1525,8';
const replaced = numStr.replace(/,/, '.');
const num = Number(replaced);

If however, your number is 1.525,8 you need to first remove the dots (str.replace(/\./g, '');).

Alxandr
  • 12,345
  • 10
  • 59
  • 95