Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an integer to decimal in JavaScript?

Tags:

I have a number in JavaScript that I'd like to convert to a money format:

556633 -> £5566.33

How do I do this in JavaScript?

like image 593
jon colins Avatar asked Jan 12 '10 10:01

jon colins


People also ask

Can an integer have a decimal JavaScript?

JavaScript has only one type of number. Numbers can be written with or without decimals.

How do you round a number to 2 decimal places in JavaScript?

Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.

Does JavaScript have a decimal type?

There is no decimal data type in JavaScript - the only numeric data type is floating-point. Therefore it is generally recommended to handle money as 2550 cents instead of 25.50 dollars.


2 Answers

Try this:

var num = 10;
var result = num.toFixed(2); // result will equal string "10.00"
like image 155
knipknap Avatar answered Sep 18 '22 13:09

knipknap


This works:

var currencyString = "£" + (amount/100).toFixed(2);
like image 27
Rob Levine Avatar answered Sep 20 '22 13:09

Rob Levine