Home > Software engineering >  How to convert a string of numbers without decimal points to a integer?
How to convert a string of numbers without decimal points to a integer?

Time:12-14

I have a string of numbers such as '9990' which in reality equals $99.90. I need to transform numbers like this to integers with decimal points. An acceptable outcome would be 99.90, 2.50, 250.25.

I have tried parseFloat((parseInt("9990")/100).toFixed(2)) and the outcome has been 99.9 leaving the zero off. Thanks for your time and looking forward to a resolution.

CodePudding user response:

The float value of 99.90 is 99.9 (trailing 0 does not have meaning). If you want to print it with 2 decimals, you need to convert it to a string:

const numberAsString = (parseInt("9990")/100).toFixed(2);
console.log(numberAsString);

Note that the toFixed method would return a string.

CodePudding user response:

You need to use toFixed to get the value to 2 places

(parseInt("9990")/100).toFixed(2);
  • Related