string - Convert a number to a duodecimal, replacing numbers to one of 12 letters in JavaScript -
I am working on a system to replace numbers with letters for listings. I use 12 letters, such as ABCEHKMOPTXY . Then such a replacement requires the use of the dodicimal number format.
Here is the table for conversion results, which is important:
1 â ???? A2 - B3 - C ... 11 - X 12 - Y13 - AA 14 - AB ... 48 - CY-49 - EA ... 156 - YY157 - AAA 158 - AAB I try to use the .toString (12) to convert the number to a doudecimal and then change each letter to switch-case I replace it with letters, but the difficulties with round doudecimal numbers, like the result of 1884.
1883 - YYX 1884 - NY ** - wrong! Eg ** 1885 - AAAA This is my job, which works well for numbers up to 1884, but I need a more universal one. Please, help! function numToString (num) {var duodecimal = (+ num) .toString (12); Var numArray = duodecimal.split (''); Var lttrs = ''; (Var i = 0; i & lt; numArray.length; i ++) {switch (numArray [i]) {case '1': lttrs + = 'A' break case '2': lttrs + = ' B 'break case' 3 ': Lttrs + =' c 'break case' 4 ': lttrs + =' e 'brake case' 5 ': lttrs + =' h 'brake case' 6 ': lttrs + =' k ' break case '7': lttrs + = 'm' break case 'b': lttr + = 'x' brake case '0': break case '8': lttrs + = 'o' brake case '9': lttrs + = 'P' brake case 'A': lttrs + lttrs = lttrs.slice (0, -1); Switch (numArray [i-1]) {case '1': if (numArray [i-2]) {lttrs = lttrs.slice (0, -1); Letter + = 'Y') Brake case '2': lttrs + = 'A' break case '3': lttrs + = 'B' break case '4': lttrs + = 'C' break case '5': lttrs Break case '6': lttrs + = 'h' break case '7': lttrs + = 'k' break case '8': lttrs + = 'm' break case '9': lttrs + 'O' break case 'a': lttrs + = 'P' brake case 'B': lttrs + = 'T' Brake case '0': lttrs = lttrs.slice (0, -1); Lttrs + = 'X' brake) lttrs + = 'y' brake}} Return lttrs; }
I think I found this:
Function n2s (n, digits) {var s = ''; Var l = digits.length; While (n> gt; 0) {s = digits.charAt ((n - 1)% l) + s; n = Math Cecil (N / L) -1; } Return S; } A check in the console of Chrome:
var i, letters = 'ABCEHKMOPTXY'; (I = 1; i & lt; = 25; i ++) for console.log (i, n2s (i, letters)); Console.log (1884, N2S (1884, letter)); // 1884 "YYY" console.log (1885, N2S (1885, letter)); Reverse Function
I have to admit that there is still some magic for me, so I'm sure what you do here I have not been able to give full information about this: - D Anyway, as you requested, this is a reverse function:
function s2n (s, digits) {var Pow, n = 0, i = 0; Whereas (i ++ & lt; s.length) {pow = Math.pow (digits.length, s.length - i); N + = (Issue index (s.charAt (i - 1)) + 1) * Pau; } Return n; } The usage case is:
var characters = 'abckhkmoptxa'; Var = s2n ('YYT', letter); Var to = s2n ('aaac', letter); For (var n = from; n & lt; = to; n ++) {console.log (n, n2s (n, letter)); } Under Chrome or Firefox:
- Press F12 .
- Copy-paste both functions
- Enter .
You should see the following lines:
1882 "YYT" 1883 "YYX" 1884 "YYY" 1885 "AAAA" 1886 "AAB" 1887 "AAAC "
Comments
Post a Comment