Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Friendly Number by mozurin
"use strict";
function friendlyNumber(number, options = {})
{
const {
base = 1000,
decimals = 0,
suffix = '',
powers = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],
} = options;
const power = Math.min(
number? Math.floor(Math.log(Math.abs(number)) / Math.log(base)) : 0,
powers.length - 1
);
return (
(number < 0? '-' : '') +
Math[decimals? 'round' : 'floor'](
(Math.abs(number) / base**power) * 10**decimals
).toString().padStart(decimals + 1, '0').replace(
new RegExp(`(.{${decimals}})$`),
e => e? '.' + e : ''
) +
powers[power] +
suffix
);
}
June 25, 2018
Comments: