Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 10, 2023 03:37
Show Gist options
  • Save code-boxx/f2640d630be19f4dbd52acc56b90ff67 to your computer and use it in GitHub Desktop.
Save code-boxx/f2640d630be19f4dbd52acc56b90ff67 to your computer and use it in GitHub Desktop.
Javascript Add Comma To Number

JAVASCRIPT ADD COMMA TO NUMBER

https://code-boxx.com/add-comma-to-numbers-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>Add Comma</title>
<script>
// (A) THE NUMBER
var num = 1234567.89;
// (B) TO LOCALE STRING
console.log(num.toLocaleString("en-US")); // 1,234,567.89
console.log(num.toLocaleString("fr-FR")); // 1 234 567,89
console.log(num.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }));
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Add Comma</title>
<script>
// CREDIT: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
// (A) THE NUMBER
var num = 1234567.89;
// (B) CONVERT TO STRING & REPLACE
var commas = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(commas); // 1,234,567.89
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Add Comma</title>
<script>
// (A) ADD COMMAS TO SEPERATE GIVEN NUMBER
// num : original number
// per : add comma per n digits (default 3)
// places : number of decimal places (default 2)
function addComma (num, per, places) {
// (A1) SET DEFAULTS
if (per==undefined) { per = 3; }
if (places==undefined) { places = 2; }
// (A2) NO DECIMAL PLACES - ROUND OFF
// REMOVE THIS IF YOU DON'T WANT TO ROUND OFF
if (places==0) { num = Math.round(num); }
// (A3) SPLIT WHOLE & DECIMAL NUMBERS
var cString = num.toString(),
cDot = cString.indexOf("."),
cWhole = "", cDec = "";
if (cDot == -1) {
cWhole = cString;
cDec = 0;
} else {
cWhole = cString.substring(0, cDot);
cDec = cString.substring(cDot+1);
}
// (A4) ADD COMMAS TO WHOLE NUMBER
var aComma = "", count = 0;
if (cWhole.length > per) { for (let i=(cWhole.length-1); i>=0; i--) {
aComma = cWhole.charAt(i) + aComma;
count++;
if (count == per && i!=0) {
aComma = "," + aComma;
count = 0;
}
}} else { aComma = cWhole; }
// (A5) ROUND OFF TO GIVEN DECIMAL PLACES
if (places==0) { cDec = ""; }
else {
cDec = +("0." + cDec);
cDec = cDec.toFixed(places).toString().substring(1);
}
// (A6) RETURN "WHOLE WITH COMMA" PLUS DECIMAL PLACES
return aComma + cDec;
}
// (B) TEST
// (B1) WHOLE NUMBER
var whole = 123456789;
console.log(addComma(whole)); // 123,456,789.00
console.log(addComma(whole, 4, 0)); // 1,2345,6789
// (B2) DECIMAL
var dec = 23456.78;
console.log(addComma(dec)); // 23,456.78
console.log(addComma(dec, 2, 0)); // 2,34,57
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment