Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 16, 2023 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/0124cb717d9c98fc3b33be7b0a0783ac to your computer and use it in GitHub Desktop.
Save code-boxx/0124cb717d9c98fc3b33be7b0a0783ac to your computer and use it in GitHub Desktop.
Javascript Format Number As Currency

JAVASCRIPT FORMAT NUMBER AS CURRENCY

https://code-boxx.com/format-number-currency-string-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Number To Currency</title>
<script>
// (A) THE NUMBER
var amount = 123456;
// (B) TO USD - $123,456.00
var usd = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
console.log(usd);
// (C) TO JPY - ¥123,456
var jpy = new Intl.NumberFormat("jp-JP", {
style: "currency",
currency: "JPY"
}).format(amount);
console.log(jpy);
// (D) EURO - €123,456.00
var eur = new Intl.NumberFormat("en-GB", {
style: "currency",
currency: "EUR"
}).format(amount);
console.log(eur);
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Number To Currency</title>
</head>
<body>
<script>
// (A) CONVERT TO CURRENCY (DOLLARS)
function tocur (amount) {
return "$" + amount
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// (B) TEST
var testA = tocur(1234567),
testB = tocur(123456.78);
console.log(testA); // $1,234,567
console.log(testB); // $123,456.78
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Number To Currency</title>
</head>
<body>
<script>
// (A) CURRENCY CONVERSION FUNCTION (DOLLARS)
function tocur (amount) {
// (A1) NUMBER TO STRING
var amount = amount.toString(),
dec = ".00",
temp = amount.indexOf(".");
// (A2) EXTRACT DECIMALS (IF ANY)
if (temp != -1) {
dec = amount.substring(temp);
amount = amount.substring(0, temp);
}
// (A3) ADD THOUSAND SEPARATORS
if (amount.length>3) {
temp = "";
for (let i=amount.length-1, j=1; i>=0; i--, j++) {
temp = amount[i] + temp;
if (j%3==0 && i!=0) { temp = "," + temp; }
}
amount = temp;
}
// (A4) RESULT
return "$" + amount + dec;
}
// (B) TEST
var testA = tocur(1234567),
testB = tocur(123456.78);
console.log(testA); // $1,234,567.00
console.log(testB); // $123,456.78
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment