Reputation: 137
I want to convert a number to a char like:
Tried to find anything to help me with but I did not had any lucky. Anybody have a sample for this using JavaScript?
Thank you!
Upvotes: 11
Views: 24702
Reputation: 43
The important part is charCodeAt which returns the ASCII value for any character.
function columnIndex(name) {
let index = 0;
name = name.toUpperCase().split('');
for (let i = name.length - 1; i >= 0; i--) {
let piece = name[i];
let colNumber = piece.charCodeAt() - 64;
index = index + colNumber * Number(Math.pow(26, name.length - (i + 1)));
}
return index;
}
['A', 'AA', 'AAA', 'AAB', 'MMMM', 'MMMN'].forEach((n) => {
console.log(n + ' : ' + columnIndex(n));
});
Upvotes: 1
Reputation: 960
I wrote a solution using recursion, well... just for fun really :)
const numberToBase26 = (val, tail = '') => {
if (val <= 26) {
return `${String.fromCharCode(val + 64)}${tail}`;
}
const remainder = val % 26 || 26;
const division = Math.trunc(val / 26) - (remainder === 26 ? 1 : 0);
return numberToBase26(division, `${String.fromCharCode(remainder + 64)}${tail}`);
};
console.log(numberToBase26(475254), ' should be equal to ZZZZ');
Upvotes: 4
Reputation: 147363
The number conversion you've shown doesn't seem consistent. It seems you want to convert a number to the equivalent spreadsheet column letter, similar to this Python question: Convert spreadsheet number to column letter.
Converting that code to javascript (and cleaning up a bit) gives:
function numToSSColumn(num){
var s = '', t;
while (num > 0) {
t = (num - 1) % 26;
s = String.fromCharCode(65 + t) + s;
num = (num - t)/26 | 0;
}
return s || undefined;
}
// A Z AA CZ DA YZ ZZ AAA
[0,1,26,27,104,105,676,702,703,
//AAZ ABA AZZ BAA BAZ BBA YYYZ ZZZZ
728,729,1378,1379,1404,1405,456976,475254].forEach(function(n) {
console.log(n + ' : ' + numToSSColumn(n));
});
The function doesn't check the input and returns undefined if n < 0
.
Your conversions don't seem to work because spreadsheet columns don't start from 0, they start from 1, so A-Z is 1 to 26, AA to AZ is 27 to 52, and so on. 676 is YZ and 456976 is YYYZ. ZZZZ is 475254 (or 11110 base26);
Upvotes: 16
Reputation: 303
You have very weird cases that are hard to understand. if 27 = AA
, then 52 = ZZ
but according to your example 676 = ZZ
. Can you elaborate on the series that you want to generate.
Just in case following is the algorithm that satisfies my example:
getAlphabetFromNumber = function (_num) {
var str = "";
multiples = Math.ceil(_num / 26);
_charAtCode = _num - ((multiples - 1) * 26)
for (let i = 0; i < multiples; i++)
str += String.fromCharCode(_charAtCode + 64);
return str;
}
Upvotes: 1
Reputation: 7777
Here's a start, open your inspector, select console. and paste this in:
cvt = function(n) {return(String.fromCharCode(n+'A'.charCodeAt(0)-1))}
Now you can type cvt(1)
to get 'A', or cvt(26)
to get 'Z'
It only works for one character. The rest is for you to work out
Upvotes: 0
Reputation: 616
You will need to write a loop, and mod (%) the number by 26 each time through and use the following:
String.fromCharCode(num + 64) // if num is 1 then result is 'A'
Upvotes: 8