0

I am currently trying to add two decimal places to the end of the number 1000 (I need it to be 1000.00)

I am trying to use: parseFloat(1000).toFixed(2) but it keeps returning a string. When I do parseFloat((1000).toFixed(2)) it returns a number, but gets rid of the decimal places. Is there a way to convert the number 1000 into the number 1000.00 without returning a string?

theunit12
  • 17
  • 3
  • 5
    There's no scenario where you need a number, that is represented with 2 empty trailing zeroes. It makes no sense to store that information. The "1000.00" the string representation of what you're trying to achieve. And you will need it when you display data to the user, but it's not needed internally. – Keimeno Mar 07 '21 at 17:22
  • whats the use case here ? why do you want it to be as 1000.00 ? – KcH Mar 07 '21 at 17:24
  • As a number, 1000.00 is **exactly** the same value as 1000. You're asking for something nonsensical. – Pointy Mar 07 '21 at 17:38
  • Its for a program that is going to work as an ATM, the decimals are needed to represent change – theunit12 Mar 07 '21 at 17:52
  • You should definitely not do "money math" with JavaScript numbers. They are binary floating point values, and you'll find it impossible to avoid results that don't make sense. There are packages out there that give you a "Decimal" type, but it's not native to the language (yet). – Pointy Mar 07 '21 at 17:54

2 Answers2

0

Try to use .toLocaleString()

var n = 1000;
var nWithZerto = n.toLocaleString("en",{useGrouping: false,minimumFractionDigits: 2});
abhjt
  • 402
  • 4
  • 11
  • 25
-1

Since, Javascript treat both integer and, decimal as Number, so it doesn't matter in calculation.

But, if you are printing it then only you require formatting and it can be done as (1000).toFixed(2)-> string.