简体   繁体   中英

How to add decimals to a number in JavaScript?

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?

Try to use .toLocaleString()

var n = 1000;
var nWithZerto = n.toLocaleString("en",{useGrouping: false,minimumFractionDigits: 2});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM