如何在HTML中将基数为10的数字转换为字母顺序列表

前端之家收集整理的这篇文章主要介绍了如何在HTML中将基数为10的数字转换为字母顺序列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将整数转换为字母等价,如 HTML中的有序列表.
  1. <ol type="a">

我试图将基数为10的数字转换为带有-z数字的基数26.

但那不是我想要的.

  1. IN WANT GET
  2. -----------------------
  3. 1 => a <= a
  4. 2 => b <= b
  5. 3 => c <= c
  6. 4 => d <= d
  7. 5 => e <= e
  8. 6 => f <= f
  9. 7 => g <= g
  10. 8 => h <= h
  11. 9 => i <= i
  12. 10 => j <= j
  13. 11 => k <= k
  14. 12 => l <= l
  15. 13 => m <= m
  16. 14 => n <= n
  17. 15 => o <= o
  18. 16 => p <= p
  19. 17 => q <= q
  20. 18 => r <= r
  21. 19 => s <= s
  22. 20 => t <= t
  23. 21 => u <= u
  24. 22 => v <= v
  25. 23 => w <= w
  26. 24 => x <= x
  27. 25 => y <= y
  28. 26 => z <= az
  29. 27 => aa <= aa
  30. 28 => ab <= ab
  31. 29 => ac <= ac
  1. private final static char[] digits = {
  2. '0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
  3. };
  4.  
  5. private static String numberToAlphaNumeric(long i,int radix) {
  6.  
  7. char[] buf = new char[65];
  8. int charPos = 64;
  9. boolean negative = (i < 0);
  10. if (!negative) {
  11. i = -i;
  12. }
  13. while (i <= -radix) {
  14. buf[charPos--] = digits[(int)(-(i % radix))];
  15. i = i / radix;
  16. }
  17. buf[charPos] = digits[(int)(-i)];
  18. if (negative) {
  19. buf[--charPos] = '-';
  20. }
  21. return new String(buf,charPos,(65 - charPos));
  22. }
  23.  
  24. public static String numberToAlphaNumeric(long number) {
  25. ArrayList<String> list = new ArrayList<String>();
  26. for( int j = 0; list.size() != number; j++ ) {
  27. String alpha = numberToAlphaNumeric( j,digits.length );
  28. if(!alpha.contains( "0" )) {
  29. list.add( alpha );
  30. }
  31. }
  32. return list.get( list.size()-1 );
  33. }

我的第二个想法:

如果我将新的前导符号扩展为数字并将我的数字转换为基数为27的数字,
我在每次携带中都有新的符号,这是错误的,我可以过滤掉它们.

这是非常低效和丑陋的,但我没有更多的想法.常见的方法是什么?

解决方法

这是基本算法.如果您需要更高效,请使用StringBuffer:
  1. public static String getAlpha(int num) {
  2.  
  3. String result = "";
  4. while (num > 0) {
  5. num--; // 1 => a,not 0 => a
  6. int remainder = num % 26;
  7. char digit = (char) (remainder + 97);
  8. result = digit + result;
  9. num = (num - remainder) / 26;
  10. }
  11.  
  12. return result;
  13. }

另一种方法是转换为base 26,然后为你得到的字符串中的每个字符添加97.

猜你在找的HTML相关文章