Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: shorten large numbers, force decimal places, and choose to represent 1000's as .001 millions

I'm trying to accomplish three things -

I want to shorten large numbers and add a K/M/B suffix I want to be able to force the number of decimal places I want to be able to force thousands to be represented as decimal fractions of a million

just shorten, round to 2 decimals

  • 1200000 ---->>> 1.2M
  • 1248000 ---->>> 1.25M
  • 248000 ---->>> 248K

shorten, force 2 decimal places

  • 1200000 ---->>> 1.20M
  • 1248000 ---->>> 1.25M
  • 248000 ---->>> 248.00K

shorten, force 3 decimal places, force thousands to millions

  • 1200000 ---->>> 1.200M
  • 1248000 ---->>> 1.248M
  • 248000 ---->>> 0.248M

I have a javascript function that I found that does much of this, except that it doesn't force the number of decimal places, and it doesn't allow me to force thousands to millions

function shortenNumber (num, decimalPlaces) {
var str,
    suffix = '';

decimalPlaces = decimalPlaces || 0;
num = +num;

var factor = Math.pow(10, decimalPlaces);


//99999 -> 99.9K

if (num < 1000) {
    str = num;
} else if (num < 1000000) {
    str = Math.floor(num / (1000 / factor)) / factor;
    suffix = 'K';
} else if (num < 1000000000) {
    str = Math.floor(num / (1000000 / factor)) / factor;
    suffix = 'M';
} else if (num < 1000000000000) {
    str = Math.floor(num / (1000000000 / factor)) / factor;
    suffix = 'B';
} else if (num < 1000000000000000) {
    str = Math.floor(num / (1000000000000 / factor)) / factor;
    suffix = 'T';
}
return str + suffix;
}

So it accomplishes the first requirement, partially accomplishes the second (it will round to 2 decimal places, but if the decimal is 0 it drops it), but can't represent K's as M's

How can I modify this function to do this (or replace it with another that does)?

Thanks!

like image 346
rolfsf Avatar asked Aug 09 '13 16:08

rolfsf


2 Answers

This should do what you are asking:

function abbreviate(number, maxPlaces, forcePlaces, forceLetter) {
  number = Number(number)
  forceLetter = forceLetter || false
  if(forceLetter !== false) {
    return annotate(number, maxPlaces, forcePlaces, forceLetter)
  }
  var abbr
  if(number >= 1e12) {
    abbr = 'T'
  }
  else if(number >= 1e9) {
    abbr = 'B'
  }
  else if(number >= 1e6) {
    abbr = 'M'
  }
  else if(number >= 1e3) {
    abbr = 'K'
  }
  else {
    abbr = ''
  }
  return annotate(number, maxPlaces, forcePlaces, abbr)
}

function annotate(number, maxPlaces, forcePlaces, abbr) {
  // set places to false to not round
  var rounded = 0
  switch(abbr) {
    case 'T':
      rounded = number / 1e12
      break
    case 'B':
      rounded = number / 1e9
      break
    case 'M':
      rounded = number / 1e6
      break
    case 'K':
      rounded = number / 1e3
      break
    case '':
      rounded = number
      break
  }
  if(maxPlaces !== false) {
    var test = new RegExp('\\.\\d{' + (maxPlaces + 1) + ',}$')
    if(test.test(('' + rounded))) {
      rounded = rounded.toFixed(maxPlaces)
    }
  }
  if(forcePlaces !== false) {
    rounded = Number(rounded).toFixed(forcePlaces)
  }
  return rounded + abbr
}

abbreviate(1200000, 2, false, false)
abbreviate(1248000, 2, false, false)
abbreviate(248000, 2, false, false)

abbreviate(1200000, 2, 2, false)
abbreviate(1248000, 2, 2, false)
abbreviate(248000, 2, 2, false)

abbreviate(1200000, 3, 3, 'M')
abbreviate(1248000, 3, 3, 'M')
abbreviate(248000, 3, 3, 'M')
like image 86
Gabe Avatar answered Oct 22 '22 12:10

Gabe


If you're not opposed to an external library, there's Numeral.js.

like image 42
Stephen Thomas Avatar answered Oct 22 '22 12:10

Stephen Thomas