Skip to content

Instantly share code, notes, and snippets.

@MartinMuzatko
Last active November 1, 2022 18:05
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save MartinMuzatko/1060fe584d17c7b9ca6e to your computer and use it in GitHub Desktop.
Save MartinMuzatko/1060fe584d17c7b9ca6e to your computer and use it in GitHub Desktop.
JS Commarization, Number to words from Million, Billion, Trillion.... and extendible
function commarize()
{
// 1e6 = 1 Million, begin with number to word after 1e6.
if (this >= 1e6)
{
var units =
[
"Million",
"Billion",
"Trillion",
"Quadrillion",
"Quintillion",
"Sextillion",
"Septillion",
"Octillion"
// ... Put others here, you can look them up here:
// http://bmanolov.free.fr/numbers_names.php
// If you prefer to automate the set of numbers, look at the number vocabulary:
// https://gist.github.com/MartinMuzatko/1b468b7596c71e83838c
// Javascript allows plain numbers to a maximum of ~1.79e308
]
// Divide to get SI Unit engineering style numbers (1e3,1e6,1e9, etc)
var unit = Math.floor((this / 1000).toFixed(0).toString().length)
// Calculate the remainder. 1,000,000 = 1.000 Mill
var num = (this / ('1e'+(unit+2))).toFixed(3)
var unitname = units[Math.floor(unit / 3) - 1]
// output number remainder + unitname
return num + ' ' + unitname
}
// Split floating number
var parts = this.toString().split(".")
// Only manipulate first part (not the float number)
// If you prefer europe style numbers, you can replace . with ,
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",")
return parts.join(".")
}
// Add method to prototype. this allows you to use this function on numbers and strings directly
Number.prototype.commarize = commarize
String.prototype.commarize = commarize
@corneyl
Copy link

corneyl commented Jan 10, 2018

The commarize function above does not work properly, unit should be multiple of 3, which is not the case. Try e.g. 12,345,678, this should yield 12.345M but it gives 1.234M.
This one should work better (and supports k):

function commarize() {
  // Alter numbers larger than 1k
  if (this >= 1e3) {
    var units = ["k", "M", "B", "T"];
    
    // Divide to get SI Unit engineering style numbers (1e3,1e6,1e9, etc)
    let unit = Math.floor(((this).toFixed(0).length - 1) / 3) * 3
    // Calculate the remainder
    var num = (this / ('1e'+unit)).toFixed(2)
    var unitname = units[Math.floor(unit / 3) - 1]
    
    // output number remainder + unitname
    return num + unitname
  }
  
  // return formatted original number
  return this.toLocaleString()
}

// Add method to prototype. this allows you to use this function on numbers and strings directly
Number.prototype.commarize = commarize
String.prototype.commarize = commarize

@picheli20
Copy link

I've simplify the Math, and added the support to say that's the min value that you want

function commarize(min) {
  min = min || 1e3;
  // Alter numbers larger than 1k
  if (this >= min) {
    var units = ["k", "M", "B", "T"];
    
    var order = Math.floor(Math.log(this) / Math.log(1000));

    var unitname = units[(order - 1)];
    var num = Math.floor(this / 1000 ** order);
    
    // output number remainder + unitname
    return num + unitname
  }
  
  // return formatted original number
  return this.toLocaleString()
}

// Add method to prototype. this allows you to use this function on numbers and strings directly
Number.prototype.commarize = commarize
String.prototype.commarize = commarize

@julianYaman
Copy link

This is only working until you reach a number higher than 1e21.

@gustavodelamou
Copy link

I just want an example in HTML code please if it's possible

@nikhilroy2
Copy link

nikhilroy2 commented Jan 15, 2020

how can I define the code inside chart js?

options: {
scales: {
yAxes: [{
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return String(value).commarize();

                }
            }
        }]
    }
}

it's not working please give me the solution

@MartinMuzatko
Copy link
Author

For the sake of history I leave this script here, but seriously. Use something like numeral.js to format numbers. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment