Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用字符串表示替换数字_Java_Android_Regex_String - Fatal编程技术网

Java 用字符串表示替换数字

Java 用字符串表示替换数字,java,android,regex,string,Java,Android,Regex,String,我正在尝试制作一个程序,它显示相当大的数字(biginger-large)。为了方便用户,我将数字显示为字符串(1 000 000=1百万)。下面的代码显示了我当前的尝试。我的问题在于,事实上,我要替换的实际数字不会是好的和圆的。该程序不需要显示100%的准确值,而是给出一个大致的数字。即: 1 234 567 = 1 Million 1 000 000 = 1 Million 1 934 234 = 1 Million 我的当前代码(为简洁起见缩短): 我想用#的替换零,这样它就可以搜索x个

我正在尝试制作一个程序,它显示相当大的数字(biginger-large)。为了方便用户,我将数字显示为字符串(1 000 000=1百万)。下面的代码显示了我当前的尝试。我的问题在于,事实上,我要替换的实际数字不会是好的和圆的。该程序不需要显示100%的准确值,而是给出一个大致的数字。即:

1 234 567 = 1 Million
1 000 000 = 1 Million
1 934 234 = 1 Million
我的当前代码(为简洁起见缩短):

我想用
#的
替换零,这样它就可以搜索
x个3位数
并替换为相应的单词,但我不知道如何实现这一点。我还应该注意到,我知道,
1000000,billion,etc
当前在最终输出字符串中向后拼写

编辑:请英国读者注意,我使用的是美国定义的
*millions


编辑2:一直在做一些谷歌搜索,发现这个-。除了我不知道如何修改正则表达式来查找
000000

之外,我只是简单地数数数字。从头开始的粗略代码:

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;
}
字符串结果=”;
Pattern=Pattern.compile(“[\\s0-9]+”);
Matcher-Matcher=pattern.Matcher(t);
int指数=0;
while(matcher.find()){
int newIndex=matcher.start();
结果+=t.子字符串(索引,新索引);
结果+=转换(matcher.group());
index=matcher.end()+1;
}
结果+=t.子字符串(索引,t.长度()-1);
专用字符串转换(字符串uglyNumber){
//去掉空白
字符串编号=uglyNumber.replace(“\\s”和“”);
//简单地数数数字

if(6
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"
} 
这应该适用于您描述的简单用法。

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

为什么不将文本解析为整数,并使用适当的显示方式对其进行格式化呢。

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

如果字符串的长度小于6,我将其保持原样,并简单地添加空白以显示外观。如果字符串的长度大于6,我根据字符串长度的模式选择前1、2或3个数字,并将它们作为实际数字显示在开始处(100万、340亿、124万亿等)

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

如果有人需要对此进行澄清,请毫不犹豫地询问我!这是pastebin的链接-


祝你好运!

将数字舍入到1000到一个有效数字的示例实现

或者,三位数的组可以用逗号或空格分隔

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;
}

您的数字是否嵌入到某个大文本中?或者您的
t
字符串中只有该内容?只有该内容。它显示的是高功率,例如(12312452355^170).再次“嗨”。你在我的最后一个问题上帮了大忙:D@Asryael..你的意思是你也有幂形式的数字吗?不,它显示的是高幂的答案
t=(12312452355^170)。toString();
(显然是Psuedo代码)如果你只寻找零,你将无法做到,例如,
1234567=100万
。为什么不只计算整数?除非myValue的值高于2^(32-1)。因此,BigInteger:Diges同样适用于BigInteger,我只是提出一个想法,不是实现好的,而是更好,例如
txt=Math.round(myValue/1e6)+“百万”
50000000
无效,可能是
5e8
。否则,看起来不错。
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"
);