2017-05-30 6 views
1

Ich brauche eine Möglichkeit, das zu tun. Ich habe schon lange gesucht, aber ich habe keine Lösung gefunden.Format Zeichenfolge [211659646] -> [211.659.646]

Ich möchte nur ein string zu formatieren, ex:

100  -> 100 
1000 -> 1.000 
10000 -> 10.000 
100000 -> 100.000 
1000000 -> 1.000.000 

und so weiter ..

Können Sie mir helfen, mit dieser Frage? Was soll ich für solche Probleme lernen?

Dank: D

+0

Mögliches Duplikat [? Wie das Dezimaltrennzeichen von DecimalFormat von Komma ändern Punkt zu Punkt /] (https://stackoverflow.com/questions/5054132/ how-to-the-decimal-Trennzeichen-von-decimalformat-from-comma-to-dot-point) –

Antwort

1

Um Punkte einfügen (.) in a String (nicht eine Zahl), bei allen 3 Positionen, am Ende beginnen, könnten Sie dies tun:

private static String format(String s) { 
    if (s.length() <= 3) 
     return s; 
    int first = (s.length() - 1) % 3 + 1; 
    StringBuilder buf = new StringBuilder(s.substring(0, first)); 
    for (int i = first; i < s.length(); i += 3) 
     buf.append('.').append(s.substring(i, i + 3)); 
    return buf.toString(); 
} 

-Test

for (String s = "1"; s.length() <= 30; s += (s.length() + 1) % 10) 
    System.out.printf("%-30s -> %s%n", s, format(s)); 

Ausgabe

1        -> 1 
12        -> 12 
123       -> 123 
1234       -> 1.234 
12345       -> 12.345 
123456       -> 123.456 
1234567      -> 1.234.567 
12345678      -> 12.345.678 
123456789      -> 123.456.789 
1234567890      -> 1.234.567.890 
12345678901     -> 12.345.678.901 
123456789012     -> 123.456.789.012 
123456789-> 1.234.567.890.123 
123456789-> 12.345.678.901.234 
123456789-> 123.456.789.012.345 
123456789-> 1.234.567.890.123.456 
123456789-> 12.345.678.901.234.567 
123456789-> 123.456.789.012.345.678 
123456789-> 1.234.567.890.123.456.789 
123456789-> 12.345.678.901.234.567.890 
123456789-> 123.456.789.012.345.678.901 
123456789-> 1.234.567.890.123.456.789.012 
123456789-> 12.345.678.901.234.567.890.123 
123456789-> 123.456.789.012.345.678.901.234 
123456789-> 1.234.567.890.123.456.789.012.345 
123456789-> 12.345.678.901.234.567.890.123.456 
123456789-> 123.456.789.012.345.678.901.234.567 
123456789-> 1.234.567.890.123.456.789.012.345.678 
123456789-> 12.345.678.901.234.567.890.123.456.789 
123456789-> 123.456.789.012.345.678.901.234.567.890 

Da Sie speziell darum gebeten, es auf einer Saite zu machen, kann es auf jeder Saite, z

Hello World! -> Hel.lo .Wor.ld! 
+0

Woow, es ist genau das, was ich suche. Vielen Dank, es funktioniert sehr gut: D – wellersonrd

1

Codebeispiel:

String number = "1000000.11"; 
double amount = Double.parseDouble(number); 
DecimalFormat f = new DecimalFormat("#,###.00"); 

System.out.printin(f.format(amount)); 

Dies funktioniert, wenn Sie einige Dezimalstellen wollen formatiert werden.

Oder können Sie

int i = 1000000; 
string s = NumberFormat.getIntegerInstance().format(i); 
1

verwenden Sie DecimalFormat wie folgt verwendet werden: es

DecimalFormat format = new DecimalFormat("#,###.000"); 

dann übergeben Sie die Nummer:

System.out.println(format.format(#your-int)); 
0

Java 8 Lösung:

private static String format(String s) { 
    int groupCount = (s.length() + 2)/3; 
    return IntStream.range(0, groupCount).boxed() 
     .map (i -> s.substring(
      Math.max(0, s.length() - 3 * (groupCount - i)), 
      s.length() - 3 * (groupCount - 1 - i))) 
     .collect(Collectors.joining(".")); 
}