将数字转换为Word android

时间:2017-01-15 19:14:05

标签: java android

 static public class ScaleUnit{
        private int exponent;
        private String[] names;
        private ScaleUnit(int exponent, String...names){
            this.exponent = exponent;
            this.names = names;
        }
        public int getExponent(){
            return exponent;
        }
        public String getName(int index){
            return names[index];
        }
    }
static private ScaleUnit[] Scale_UNITS = new ScaleUnit[]{
            new ScaleUnit(6, "Million"),
            new ScaleUnit(9, "Billion"),
            new ScaleUnit(12, "Trillion"),
            new ScaleUnit(15, "Quadrillion"),
            new ScaleUnit(18, "Quintillion"),
            new ScaleUnit(21, "Sextillion"),
            new ScaleUnit(24, "Septillion"),
            new ScaleUnit(27, "Octillion"),
            new ScaleUnit(30, "Nonillion"),
            new ScaleUnit(33, "Decillion"),
            new ScaleUnit(36, "Undecillion"),
            new ScaleUnit(39, "Duodecillion"),
            new ScaleUnit(42, "Tredecillion"),
            new ScaleUnit(45, "Quattuordecillion"),
            new ScaleUnit(48, "Quinquadecillion"),
            new ScaleUnit(51, "Sedecillion"),
            new ScaleUnit(54, "Septendecillion"),
            new ScaleUnit(57, "Octodecillion"),
            new ScaleUnit(60, "Novendecillion"),
            new ScaleUnit(63, "Vigintillion"),
            new ScaleUnit(66, "Unvigintillion"),
            new ScaleUnit(69, "Duovigintillion"),
            new ScaleUnit(72, "Tresvigintillion"),
            new ScaleUnit(75, "Quattuorvigintillion"),
            new ScaleUnit(78, "Quinquavigintillion"),
            new ScaleUnit(81, "Sesvigintillion"),
            new ScaleUnit(84, "Septemvigintillion"),
            new ScaleUnit(87, "Octovigintillion"),
            new ScaleUnit(90, "Novemvigintillion"),
            new ScaleUnit(93, "Trigintillion"),
            new ScaleUnit(96, "Untrigintillion"),
            new ScaleUnit(99, "Duotrigintillion"),
            new ScaleUnit(102, "Trestrigintillion"),
            new ScaleUnit(105, "Quattuortrigintillion"),
            new ScaleUnit(108, "Quinquatrigintillion"),
            new ScaleUnit(111, "Sestrigintillion"),
            new ScaleUnit(114, "Septentrigintillion"),
            new ScaleUnit(117, "Octotrigintillion"),
            new ScaleUnit(120, "Noventrigintillion"),
            new ScaleUnit(123, "Quadragintillion"),
            new ScaleUnit(153, "Quinquagintillion"),
            new ScaleUnit(183, "Sexagintillion"),
            new ScaleUnit(213, "Septuagintillion"),
            new ScaleUnit(243, "Octogintillion"),
            new ScaleUnit(273, "Nonagintillion"),
            new ScaleUnit(303, "Centillion"),
            new ScaleUnit(306, "Uncentillion"),
            new ScaleUnit(309, "Duocentillion"),
            new ScaleUnit(312, "Trescentillion"),
            new ScaleUnit(333, "Decicentillion"),
            new ScaleUnit(336, "Undecicentillion"),
            new ScaleUnit(363, "Viginticentillion"),
            new ScaleUnit(366, "Unviginticentillion"),
            new ScaleUnit(393, "Trigintacentillion"),
            new ScaleUnit(423, "Quadragintacentillion"),
            new ScaleUnit(453, "Quinquagintacentillion"),
            new ScaleUnit(483, "Sexagintacentillion"),
            new ScaleUnit(513, "Septuagintacentillion"),
            new ScaleUnit(543, "Octogintacentillion"),
            new ScaleUnit(573, "Nonagintacentillion"),
            new ScaleUnit(603, "Ducentillion"),
            new ScaleUnit(903, "Trecentillion"),
            new ScaleUnit(1203, "Quadringentillion"),
            new ScaleUnit(1503, "Quingentillion"),
            new ScaleUnit(1803, "Sescentillion"),
            new ScaleUnit(2103, "Septingentillion"),
            new ScaleUnit(2403, "Octingentillion"),
            new ScaleUnit(2703, "Nongentillion"),
            new ScaleUnit(3003, "Millinillion"),

    };
static public enum Scale {
        SHORT;

        public String getName(int exponent){
            for (ScaleUnit unit: Scale_UNITS){
                if (unit.getExponent() == exponent){
                    return unit.getName(this.ordinal());
                }
            }
            return "";
        }
    }

我有这个清单和代码。我想删除数字并转换单词。比如160392 = 1Million或79938384472 = 79Billion。 我认为如果我有正确的数字匹配指数它会拉出单词,但如果不匹配指数,它返回null。

3 个答案:

答案 0 :(得分:0)

除以3并乘以3得到最接近的“有效”指数值,如下所示:

    public String getName(int exponent){
        exponent = (exponent / 3) * 3;
        for (ScaleUnit unit: Scale_UNITS){
            if (unit.getExponent() == exponent){
                return unit.getName(this.ordinal());
            }
        }
        return "";
    }

答案 1 :(得分:0)

建议:确保比例单位数组按指数排序(我相信它在您的代码中)。将数字转换为字符串,例如"79938384472"。找到具有最大指数的比例单位,严格小于字符串的lentgh。在示例中,长度为11,因此您需要ScaleUnit(9, "Billion")。从该比例单位中取出指数,从字符串末尾剥离这么​​多个字符(79938384472变为79)。将截断的字符串与缩放单元中的名称连接起来。

答案 2 :(得分:0)

也许你想要采用略微不同的,IMO更清晰的方法:

将指数范围映射到单词。

例如,您可以将[6-8]映射到"百万",[9-11]到" Billion"等等 为此,您可以使用Guavas RangeMap

RangeMap<Integer, String> expWordRanges =
     ImmutableRangeMap.builder()
                      .put(Range.closedOpen(6,9), "Million")
                      .put(Range.closedOpen(9,12), "Billion")
                      // etc.
                      .build();
String word = expWordRanges.get(exponent);

然而,虽然关注点分离通常是件好事,但这里的设计披露你必须将实际数字分解两次:

  1. 将指数传递给单词确定方法
  2. 隐含地得到余数 - 这取决于单词方法的结果
  3. 示例:

    鉴于21 277 185 应该会产生2100万。

    你现在

    1. 获取地点数量(您的&#39;指数&#39;)= 8
    2. 将其传递给word method = Million
    3. 计算前缀,例如通过千分之一直到小于千(指数除以3次迭代/平铺,向下舍入 - 用指数进行第二次触摸)。参看到@Ole V.V。的回答
        

      找到指数最大的比例单位,严格小于字符串的长度

    4. 步骤3是独立的,可以作为第一步完成。如果现在计算迭代次数并将其乘以3,则可以获得传递给原始getName方法所需的数字。此外,你现在不需要两次分解数字。

      鉴于整数在Java中有限,我想你已经在这里处理了Strings,如@Ole V.V.也暗示。因此,分解显然分别处理字符串的长度而不是10千的幂。这让我回到了我在帖子开头提到的解决方案。