Skip to content

Instantly share code, notes, and snippets.

@izolate
Last active November 12, 2015 22:44
Show Gist options
  • Save izolate/72f0227dd14049733fdd to your computer and use it in GitHub Desktop.
Save izolate/72f0227dd14049733fdd to your computer and use it in GitHub Desktop.
Pretty numbers
'use strict'
const suffixes = new Map()
suffixes.set(3, 'K')
suffixes.set(6, 'M')
suffixes.set(9, 'B')
suffixes.set(12, 'T')
// Figure out the appropriate scale for the number
const scale = (num) => {
let zeroes
for (let z of suffixes.keys()) if (num > z) zeroes = z
return { suffix: suffixes.get(zeroes), zeroes }
}
// Figure out how many digits in the integer
const digits = (num => Math.floor(Math.log10(Math.abs(num))) + 1)
// Make sure value is a number
const validate = (num => {
num = parseFloat(num)
if (isNaN(num)) throw new Error('Not a number')
else return num
})
module.exports = (ugly, decimal) => {
ugly = validate(ugly)
const d = digits(ugly), s = scale(d)
const pretty = ugly/Math.pow(10, s.zeroes)
decimal = (pretty % 1 === 0) ? 1 : (decimal + 1) || 2
if (ugly < 1000) return ugly
else return `${parseFloat(pretty.toPrecision(decimal))}${s.suffix}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment