Skip to content

Instantly share code, notes, and snippets.

@thiagopromano
Last active May 29, 2023 09:52
Show Gist options
  • Save thiagopromano/f2db87296b16f6e262a0818c3c5c246d to your computer and use it in GitHub Desktop.
Save thiagopromano/f2db87296b16f6e262a0818c3c5c246d to your computer and use it in GitHub Desktop.
var SI_PREFIX = {
'-24': 'y',
'-21': 'z',
'-18': 'a',
'-15': 'f',
'-12': 'p',
'-9': 'n',
'-6': 'μ',
'-3': 'm',
'0': '',
'3': 'k',
'6': 'M',
'9': 'G',
'12': 'T',
'15': 'P',
'18': 'E',
'21': 'Z',
'24': 'Y'
};
var E12 = [100, 120, 150, 180, 220, 270, 330, 390, 470, 560, 680, 820, 1000];
var E24 = [100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910, 1000];
var E48 = [100, 105, 110, 115, 121, 127, 133, 140, 147, 154, 162, 169, 178, 187, 196, 205, 215, 226, 237, 249, 261, 274,
287, 301, 316, 332, 348, 365, 383, 402, 422, 442, 464, 487, 511, 536, 562, 590, 619, 649, 681, 715, 750, 787, 825,
866, 909, 953, 1000];
var E96 = [100, 102, 105, 107, 110, 113, 115, 118, 121, 124, 127, 130, 133, 137, 140, 143, 147, 150, 154, 158, 162, 165,
169, 174, 178, 182, 187, 191, 196, 200, 205, 210, 215, 221, 226, 232, 237, 243, 249, 255, 261, 267, 274, 280, 287,
294, 301, 309, 316, 324, 332, 340, 348, 357, 365, 374, 383, 392, 402, 412, 422, 432, 442, 453, 464, 475, 487, 499,
511, 523, 536, 549, 562, 576, 590, 604, 619, 634, 649, 665, 681, 698, 715, 732, 750, 768, 787, 806, 825, 845, 866,
887, 909, 931, 953, 976, 1000];
/**
* Transforms the value to engineering units, for example: 1234 to 1.234kΩ
* @param {4700} value The value to get transformed
* @param {"Ω"} unit Optional unit, such as "Ω"
* @param {2} significantDigits How many significant digits to show, default is 10
* @returns {String} The input transformed, such as "4.7Ω"
* @customfunction
*/
function WithPrefix(value, unit, significantDigits) {
if (!unit)
unit = '';
if (!significantDigits)
significantDigits = 10;
var exponent = Math.floor(Math.log(Math.abs(value)) / Math.log(10));
var prefix = false;
do {
if (exponent in SI_PREFIX && (true || (exponent % 3 === 0))) {
prefix = SI_PREFIX[exponent];
} else {
--exponent;
if (exponent < -24) {
exponent = -24;
}
}
} while (prefix === false);
var result = (value / Math.pow(10, exponent));
if (significantDigits)
result = Number(result.toPrecision(significantDigits));
if (prefix !== false) {
result += prefix;
return result + unit;
}
}
/**
* Transforms the value closest commercial value according to standard EIA, for example: 1234 to 1.2kΩ
* @param {4700} value The value to get transformed
* @param {"E24"} series EIA series, default is E24, supports E12, E24, E48 and E96
* @param {"Ω"} unit Optional unit, such as "Ω"
* @returns {string} The input transformed, such as "4.7k"
* @customfunction
*/
function GetClosestResistor(value, series, unit) {
var multiplier = 1;
var standardValueList = [];
if (!series)
standardValueList = E24;
else if (series === 'E96')
standardValueList = E96;
else if (series === 'E48')
standardValueList = E48;
else if (series === 'E24')
standardValueList = E24;
else if (series === 'E12')
standardValueList = E12;
else throw 'Invalid Series, should be "E24" or other';
while (true) {
if (value < 100) {
value = value * 10;
multiplier = multiplier / 10;
} else if (value >= 1000) {
value = value / 10;
multiplier = multiplier * 10;
} else break;
}
var closest = standardValueList.reduce(function (prev, curr) {
return (Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
});
var result = closest * multiplier;
return result;
}
@Not-A-Normal-Robot
Copy link

any chance this will be extended for the new prefixes (quecto, ronto; ronna, quetta)?

@thiagopromano
Copy link
Author

Hey @Not-A-Normal-Robot, as this is not a library, you can manually add the prefixes according to your needs when copying to your project 😄

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