convert numbers to letters
a=>1;b=>2;.aa=>27, etc.
The essence of this problem is base conversion , and the principle of base conversion is
aaaa = 1×26^3 + 1×26^2 + 1×26^1 + 1×26^0 = 18279
abcd = 1×26^3 + 2×26^2 + 3×26^1 + 4×26^0 = 19010
This gives the formula for converting letters to numbers. First get the length n of the letter string, and then convert the single string to a number (1-26).
Such as abcdef, it can be known that n=6, conversion:
1. a×26^(n-1) + b×26^(n-2) + c×26^(n-3) + d×26^(n-4) + e×26^(n-5) + f×26^(n-6)
2. 1×26^(5) + 2×26^(4) + 3×26^(3) + 4×26^(2) + 5×26^(1) + 6×26^(0)
According to this, the calculation process of converting numbers into letters can be reversed (realized by taking the remainder , note that when the remainder m is 0, it must be reset to 26, because the letter corresponding to 0 is z), and any natural number x greater than 0 has :
const number_to_word = (x) => {
let s = "";
while (x > 0) {
let m = x % 26;
m = m === 0 ? (m = 26) : m;
s = String.fromCharCode(96 + parseInt(m)) + s;
x = (x - m) / 26;
}
return s;
});
};