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 Here is the table for conversion results, which is important: I try to use the This is my job, which works well for numbers up to 1884, but I need a more universal one. Please, help! I think I found this: A check in the console of Chrome: 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: The usage case is: Under Chrome or Firefox: You should see the following lines: ABCEHKMOPTXY . Then such a replacement requires the use of the dodicimal number format.
1 â ???? A2 - B3 - C ... 11 - X 12 - Y13 - AA 14 - AB ... 48 - CY-49 - EA ... 156 - YY157 - AAA 158 - AAB
.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
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; }
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; }
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));
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; }
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)); }
1882 "YYT" 1883 "YYX" 1884 "YYY" 1885 "AAAA" 1886 "AAB" 1887 "AAAC "
Comments
Post a Comment