0

I've got a variable with string, lets say it is something like: const test = '100,71648192'; What i want to do is first change it to number and then use Math.trunc(test*100)/100

I tried parseFloat(test)Math.trunc(test * 100) / 100) but of course it is no working and i know why, but i dont know how i can do it in 1 line

Ty for your answers

Wojtek
  • 1
  • `parseFloat` doesn't recognize commas as decimal separators. `const test = Math.trunc(parseFloat('100,71648192'.replace(',', '.'))*100)/100` – pilchard Jan 24 '23 at 22:07
  • and [Parsing numbers with a comma decimal separator in JavaScript](https://stackoverflow.com/questions/18405178/parsing-numbers-with-a-comma-decimal-separator-in-javascript) – pilchard Jan 24 '23 at 22:08
  • Thank you for help, i mean i did have "." not "," initially, but i changed the position of these 2 function and that worked perfectly for me ;) – Wojtek Jan 24 '23 at 22:42
  • Great, glad it helped. Yes, you need to *pass* the result of `parseFloat` to `Math.trunc`, not chain them. – pilchard Jan 24 '23 at 22:48

0 Answers0