1

I need a way to do that. I've already searched for a long time, but I didn't find a solution.

I just want to format a string, ex:

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

and so on..

Can you help me with this question? what should I to study for problems like that?

thanks :D

Mohd
  • 5,523
  • 7
  • 19
  • 30
wellersonrd
  • 37
  • 1
  • 5
  • Possible duplicate of [How to change the decimal separator of DecimalFormat from comma to dot/point?](https://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point) – Morrison Chang May 30 '17 at 00:19

4 Answers4

3

To insert dots (.) into a String (not a number), at every 3 positions, starting at the end, you could do this:

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

Output

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
1234567890123                  -> 1.234.567.890.123
12345678901234                 -> 12.345.678.901.234
123456789012345                -> 123.456.789.012.345
1234567890123456               -> 1.234.567.890.123.456
12345678901234567              -> 12.345.678.901.234.567
123456789012345678             -> 123.456.789.012.345.678
1234567890123456789            -> 1.234.567.890.123.456.789
12345678901234567890           -> 12.345.678.901.234.567.890
123456789012345678901          -> 123.456.789.012.345.678.901
1234567890123456789012         -> 1.234.567.890.123.456.789.012
12345678901234567890123        -> 12.345.678.901.234.567.890.123
123456789012345678901234       -> 123.456.789.012.345.678.901.234
1234567890123456789012345      -> 1.234.567.890.123.456.789.012.345
12345678901234567890123456     -> 12.345.678.901.234.567.890.123.456
123456789012345678901234567    -> 123.456.789.012.345.678.901.234.567
1234567890123456789012345678   -> 1.234.567.890.123.456.789.012.345.678
12345678901234567890123456789  -> 12.345.678.901.234.567.890.123.456.789
123456789012345678901234567890 -> 123.456.789.012.345.678.901.234.567.890

Since you specifically asked for doing it on a string, it can do it on any string, e.g.

Hello World! -> Hel.lo .Wor.ld!
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

Code Sample:

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

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

This works if you want some decimal points to be formatted.

Or you can use

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

you can use DecimalFormat as the following:

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

then pass it the number:

System.out.println(format.format(#your-int));
Mohd
  • 5,523
  • 7
  • 19
  • 30
0

Java 8 solution:

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("."));
}
ajb
  • 31,309
  • 3
  • 58
  • 84