e2rabi
e2rabi

Reputation: 4848

Why casting from string to float using parseFloat dosn't give the correct result

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

Upvotes: 1

Views: 621

Answers (1)

Kyle Becker
Kyle Becker

Reputation: 1450

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.

Upvotes: 2

Related Questions