1

I'm trying to cast a value from string to decimal but it doesn't give me the correct value this my code :

  operation['montant'] =  "12 000,00" ;
    var value = parseFloat((operation['montant']).replace(",", "."));
    alert(value);

alert(value) give me only 12

Can someone help me thank you in advance

3
  • 5
    The space character terminates the number at that point. Consult the docs: parseFloat "...If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed." Commented Apr 28, 2017 at 16:09
  • 3
    ^ which means, remove spaces from the string as well. Commented Apr 28, 2017 at 16:10
  • That's right I removed the space and it's work thanx all Commented Apr 28, 2017 at 19:08

1 Answer 1

2
operation['montant'] =  "12 000,00" ;
    var value = parseFloat((operation['montant']).replace(",", ".").replace(" ", ""));
    alert(value);

This will replace spaces with, well, nothing. JS's parse float stops trying to parse something at a white space, so you were only getting 12. Hope this works for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.