11

I want to convert a number to a char like:

  • if number = 1 then A
  • if number = 26 then Z
  • if number = 27 then AA
  • if number = 676 then ZZ
  • if number = 456976 then ZZZZ

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!

3
  • what about 0? (how) do you convert that? Commented Aug 20, 2017 at 23:04
  • This makes no sense. Is it only to be done with A and Z? Or were those just "corner case examples"? How would you get AB? DE? XYZ? Commented Aug 20, 2017 at 23:14
  • Where is the logic here ( 26 + 1 == AA ) && (26 * 26 == ZZ) ? Commented Aug 20, 2017 at 23:22

6 Answers 6

16

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);

Sign up to request clarification or add additional context in comments.

Comments

8

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'

Comments

4

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');

Comments

1

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;
}

Comments

1

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));
});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.