0

我正在尝试制作一个显示相当大数字(BigInteger large)的程序。为了方便用户,我将数字显示为字符串(1 000 000 = 1 百万)。我下面的代码显示了我当前的尝试。我的问题在于,我要替换的实际数字不会很好而且很圆。这个程序不需要显示100% 准确的值,而是给出一个球场图。IE:

1 234 567 = 1 Million
1 000 000 = 1 Million
1 934 234 = 1 Million

我当前的代码(为简洁而缩短):

            if (!displayNumbers) {
                StringBuffer sb = new StringBuffer(_combinations);
                String t = sb.reverse().toString();

                Toast.makeText(getApplicationContext(), t, 1).show();

                ...

                if (t.contains("000 000 000 000 000")) {
                    t.replace("000 000 000 000 000", "quadrillion");
                }

                if (t.contains("000 000 000 000")) {
                    t.replace("000 000 000 000", "trillion");
                }

                if (t.contains("000 000 000")) {
                    t.replace("000 000 000", "billion");
                }

                if (t.contains("000 000")) {
                    t.replace("000 000", "million");
                }

                sb = new StringBuffer(t);
                _combinations = sb.reverse().toString();
            }

我正在考虑将零替换为这样的东西,#'s以便它只会搜索x lots of 3 digits并替换为相应的单词,但我不知道如何实现这一点。我还应该注意,我知道million, billion, etc当前在最终输出字符串中是向后拼写的。

编辑:应该为英国读者注意我正在使用美国定义的*illions.

编辑 2: 做了更多的谷歌搜索,发现了这个 - java Regex: replace all numeric values with one number。除了我不知道如何修改正则表达式来查找000 000.

4

5 回答 5

0

您可以使用 if 和 else 语句来做到这一点,据我了解,您并不是在寻找精确的表示,因此可以说 1400000 为 100 万。为此,您可以使用如下代码:

int myValue
String txt = null;
if (myValue> 500 000 000){
       txt = String.ValueOf((int) myValue/1000000000) + " Billion"
} else if (myValue> 500 000){
       txt = String.ValueOf((int) myValue/1000000) + " Million"
} else if (myValue> 500) {
       txt = String.ValueOf((int) myValue/1000) + " Thousand"
} 

这应该适用于您描述的简单用法。

于 2013-01-30T09:50:23.723 回答
0

您的号码(文本)仅用于显示目的。在将数字转换为字符串后对其进行操作是个坏主意。

您为什么不将文本解析为整数并使用正确的显示方式对其进行格式化。 这是一个你应该看看的例子

于 2013-01-30T09:52:09.980 回答
0

我要感谢其他所有人对如何解决这个问题的建议,我相信他们都是可能的。但是,我发现最好的方法仍然是将 BigInteger 转换为字符串,而不是添加空格。

如果字符串的长度小于 6,我将其保留原样并简单地添加空格以进行外观。如果它比 6 长,我会根据字符串长度的 Mod 取前 1、2 或 3 个数字,并将它们用作在开始时显示的实际数字(100 万、340 亿、124 万亿等)。

然后查看剩余字符串的长度,如果长度超过一定数量,只需将更多字符串添加到显示文本中。

如果有人需要澄清这一点,请不要犹豫,问我!这是 pastebin 的链接 - http://pastebin.com/3X681UkG

祝你好运!

于 2013-01-30T11:07:41.563 回答
0

将超过一千的数字四舍五入到一个有效数字的示例实现。

可选地,三位数字组可以用逗号或空格分隔。

private final static String[] illions = { 
    "m", "b", "tr", "quadr", "quint", "sext", "sept", "oct", "non", "dec",
    "undec", "duodec", "tredec", "quattuordec", "quindec", "sexdec",
    "septendec", "octodec", "novemdec", "vigint", "unvigint", "duovigint", 
    "trevigint", "quattuorvigint", "quinvigint", "sexvigint", "septenvigint",
    "octovigint", "novemvigint", "trigint", "untrigint", "duotrigint" 
};

private static String approximate( String n ) {  
    String approx = n;

    if ( n != null && n.matches( "^\\d{1,3}[\\s,]?(\\d{3}[\\s,]?)*\\d{3}$" ) ) {  
        n = n.replaceAll( "[\\s,]", "" );            
        int i = n.length() + 2;

        if ( i < 105 ) {
            int rnd = (int) Math.round( Double.parseDouble( n.substring( 0, 2 ) ) / 10 )
                    * (int) Math.pow(10, i % 3);
            n = i > 8 ? illions[ i / 3 - 3 ] + "illion" : "thousand";   
            approx = rnd  + " " + n;
        }
    }

    return approx;
}

测试

System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
    approximate("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"),  // "1000 duotrigintillion"    
    approximate("4857476486598746598743265987426598724365987265987324598726598734625987564987456"),  // "5 quinvigintillion"
    approximate("56843584275874587243582837465847326554"),  // "60 undecillion"
    approximate("1 345 678 910 111 213"),                   // "1 quadrillion"
    approximate("47,648,658,437,651"),                      // "50 trillion"
    approximate("9 891 011 123"),                           // "10 billion"
    approximate("687654321"),                               // "700 million"
    approximate("32 456 999"),                              // "30 million"
    approximate("2,678,910"),                               // "3 million"
    approximate("1 234 567"),                               // "1 million"
    approximate("123456"),                                  // "100 thousand"
    approximate("17,654"),                                  // "20 thousand"
    approximate("8765"),                                    // "9 thousand"
    approximate("654")                                      // "654"
);      
于 2013-01-30T11:31:14.837 回答
-1

我只是简单地计算数字。从头开始的粗略代码:

String result = "";
Pattern pattern = Pattern.compile("[\\s0-9]+");
Matcher matcher = pattern.matcher(t);
int index = 0;
while (matcher.find()) {
    int newIndex = matcher.start();
    result += t.substring(index, newIndex);
    result += convert(matcher.group());

    index = matcher.end() + 1;
}

result += t.substring(index, t.length() - 1);


private String convert(String uglyNumber) {

    // get rid of whitespaces
    String number = uglyNumber.replace("\\s", "");

    // simply count digits
    if( 6 < number.length() && number.length() <= 9 ) {
    return "million";
    } else if( 9 < number.length() && number.length() <= 12 ) {
        return "million";
    } else ...

    return ulgyNumber;
}

如果数字比数字和空格的简单混合更复杂,您可能想看看这个正则表达式站点

于 2013-01-30T09:48:45.913 回答