我正在尝试制作一个显示相当大数字的程序(BigInteger大)。为了方便用户,我将数字显示为字符串(1 000 000 = 1百万)。我的下面代码显示了我当前的尝试。我的问题在于,我要替换的实际数字不会很好而且很圆。此程序不需要显示 100%准确的值,而是给出一个球场图。即:
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 numerical values with one number。除了我不知道如何修改正则表达式以查找000 000
。
答案 0 :(得分:0)
你可以使用if和else语句来做,根据我的理解你不是在寻找精确的表示,所以可以说1400万的1百万。 为此,您可以使用以下代码:
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"
}
这应该适用于您所描述的简单用法。
答案 1 :(得分:0)
您的号码(文字内容)仅供展示之用。将数字转换为字符串后操作数字是个坏主意。
为什么不直接将文本解析为整数并使用适当的显示方式对其进行格式化。 Here is a example you should take a look
答案 2 :(得分:0)
我要感谢其他人的帮助,提出了解决这个问题的建议,我相信他们都是可能的。但是,我发现最好的方法是将BigInteger转换为字符串,但不来添加空格。
如果字符串的长度小于6,我就把它保留原样并简单地添加空格用于外观。如果它长于6,我根据字符串长度的Mod取出前1,2或3个数字,并将它们用作开始时显示的实际数字(1百万,34亿,124万亿等)。
然后查看剩余字符串的持续时间,并在显示文本中添加更多字符串(如果它超过一定量)。
如果有人需要澄清这一点,请不要犹豫,问我!以下是pastebin的链接 - http://pastebin.com/3X681UkG
祝你好运!答案 3 :(得分: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"
);
答案 4 :(得分:-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;
}
如果数字比数字和空格的简单组合更加复杂,您可能需要查看此regex site: