javascript使用字母表示自然数

it2023-12-30  68

javascript使用字母表示自然数

1.方法:

function createCellPos(n){ var orda = 'a'.charCodeAt(0); //97 var ordz = 'z'.charCodeAt(0); //122 var len = ordz - orda + 1; //长多26 var s = ''; while( n>= 0 ){ s = String.fromCharCode(n % len + orda) + s; n = Math.floor(n / len) - 1; } return s; }

2.调用

createCellPos(0) //a createCellPos(1) //b ... createCellPos(26)//aa createCellPos(27)//ab ...

3.注意:

str.charCodeAt(n):获取n在字符串字母在Unicode编码得位置 String.fromCharCode(number,number,...,number):number在Unicode对应的编码
最新回复(0)