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!
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) –