javascript numeric of a alphabet code example
Example 1: javascript number in alphabet
function numToSSColumn(num){
let s = '', t;
while (num > 0) {
t = (num - 1) % 26;
s = String.fromCharCode(65 + t) + s;
num = (num - t)/26 | 0;
}
return s || undefined;
}
numToSSColumn(0);
numToSSColumn(1);
numToSSColumn(26);
numToSSColumn(-1);
numToSSColumn(27);
numToSSColumn(475254);
Example 2: javascript alphabet to number
function convertLetterToNumber(str) {
str = str.toUpperCase();
let out = 0, len = str.length;
for (pos = 0; pos < len; pos++) {
out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
}
return out;
}
convertLetterToNumber("A");
convertLetterToNumber("b");
convertLetterToNumber("Ba");
convertLetterToNumber("dE");
convertLetterToNumber("ZZZZ");