Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert number to alphabet letter

I want to convert a number to its corresponding alphabet letter. For example:

1 = A
2 = B
3 = C

Can this be done in javascript without manually creating the array? In php there is a range() function that creates the array automatically. Anything similar in javascript?

like image 910
xpedobearx Avatar asked Mar 21 '16 11:03

xpedobearx


People also ask

How do I convert numbers to alphabets in Excel?

Use the SpellNumber function in individual cells Type the formula =SpellNumber(A1) into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber(22.50). Press Enter to confirm the formula.

WHAT IS A to Z in numbers?

Conversion Table A = 1 B = 2 C = 3 D = 4 E = 5 F = 6 G = 7 H = 8 I = 9 J = 10 K =11 L = 12 M = 13 N =14 O =15 P = 16 Q =17 R =18. Page 1. Conversion Table. A = 1.

Can numbers be translated to letters?

Convert numbers to letters in various formats. Numbering the letters so A=1, B=2, etc is one of the simplest ways of converting them to numbers. This is called the A1Z26 cipher. However, there are more options such as ASCII codes, tap codes or even the periodic table of elements to decode numbers.


2 Answers

Yes, with Number#toString(36) and an adjustment.

var value = 10;

document.write((value + 9).toString(36).toUpperCase());
like image 100
Nina Scholz Avatar answered Oct 21 '22 11:10

Nina Scholz


Snippet below turns the characters in the alphabet to work like numerical system

1 = A
2 = B
...
26 = Z
27 = AA
28 = AB
...
78 = BZ
79 = CA
80 = CB

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var result = ""
function printToLetter(number){
    var charIndex = number % alphabet.length
    var quotient = number/alphabet.length
    if(charIndex-1 == -1){
        charIndex = alphabet.length
        quotient--;
    }
    result =  alphabet.charAt(charIndex-1) + result;
    if(quotient>=1){
        printToLetter(parseInt(quotient));
    }else{
        console.log(result)
        result = ""
    }
}

I created this function to save characters when printing but had to scrap it since I don't want to handle improper words that may eventually form

like image 38
esantos Avatar answered Oct 21 '22 09:10

esantos