Skip to content

Instantly share code, notes, and snippets.

@owenversteeg
Created February 7, 2014 02:22
Show Gist options
  • Save owenversteeg/8856431 to your computer and use it in GitHub Desktop.
Save owenversteeg/8856431 to your computer and use it in GitHub Desktop.
Turn SI prefixes into multipliers
function makeUnprefixed(siUnitWithPrefix) {
console.log(siUnitWithPrefix);
var fullPrefixes = ['yotta', 'zeta', 'exa', 'peta', 'tera', 'giga', 'mega', 'kilo', 'hecto', 'deca', 'deci', 'centi', 'milli', 'micro', 'nano', 'pico', 'femto', 'atto', 'zepto', 'yocto']
var prefixes = ['Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'k', 'h', 'da', 'd', 'c', 'm', 'u', 'n', 'p', 'f', 'a', 'z', 'y'];
var factors = [24, 21, 18, 15, 12, 9, 6, 3, 2, 1, -1, -2, -3, -6, -9, -12, -15, -18, -21, -24];
siUnitWithPrefix = siUnitWithPrefix.toLowerCase();
var multiplier = 1;
for (var i = 0; i < fullPrefixes.length; i++) {
if (siUnitWithPrefix.indexOf(fullPrefixes[i]) === 0) {
multiplier = Math.pow(10, factors[i]);
break;
} else if (siUnitWithPrefix.indexOf(factors[i]) === 0) {
multiplier = Math.pow(10, factors[i]);
break;
}
}
return multiplier;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment