Reputation: 453
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
Reputation: 337
Using template literals you can achieve this:
const num = 556633;
const formattedNum = `${num/100}.00`;
console.log(formattedNum);
Upvotes: -1
Reputation: 6154
Try this:
var num = 10;
var result = num.toFixed(2); // result will equal string "10.00"
Upvotes: 56
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
Reputation: 41298
This works:
var currencyString = "£" + (amount/100).toFixed(2);
Upvotes: 17