jon colins
jon colins

Reputation: 453

How do I convert an integer to decimal in JavaScript?

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?

Upvotes: 24

Views: 69759

Answers (5)

Shailesh
Shailesh

Reputation: 337

Using template literals you can achieve this:

const num = 556633;
const formattedNum = `${num/100}.00`;
console.log(formattedNum); 

Upvotes: -1

knipknap
knipknap

Reputation: 6154

Try this:

var num = 10;
var result = num.toFixed(2); // result will equal string "10.00"

Upvotes: 56

Guven
Guven

Reputation: 71

This script making only integer to decimal. Seperate the thousands

onclick='alert(MakeDecimal(123456789));' 


function MakeDecimal(Number) {
        Number = Number + "" // Convert Number to string if not
        Number = Number.split('').reverse().join(''); //Reverse string
        var Result = "";
        for (i = 0; i <= Number.length; i += 3) {
            Result = Result + Number.substring(i, i + 3) + ".";
        }
        Result = Result.split('').reverse().join(''); //Reverse again
        if (!isFinite(Result.substring(0, 1))) Result = Result.substring(1, Result.length); // Remove first dot, if have.
        if (!isFinite(Result.substring(0, 1))) Result = Result.substring(1, Result.length); // Remove first dot, if have.
        return Result;

    }

Upvotes: 4

Rob Levine
Rob Levine

Reputation: 41298

This works:

var currencyString = "£" + (amount/100).toFixed(2);

Upvotes: 17

YOU
YOU

Reputation: 123841

Try

"£"+556633/100

Upvotes: 6

Related Questions