2

A little question about DecimalFormat in Java.

I need to display number 178500.59999 like (178 500,599 99)

I tried to use new DecimalFormat(format = ###,##0.00) and result was 178 500,59

How can I do it with DecimalFormat to display the all fractional part(the part can has any length) + the fractional part was formatted like 178 500,599 99

Sky
  • 521
  • 1
  • 5
  • 14
  • maybe duplicate of https://stackoverflow.com/questions/40304462/decimalformat-keep-all-decimal-numbers – Luca T. Jan 15 '18 at 12:15
  • that doesn't look like a duplicate, as he needs to use a thousand separator in the decimal part of the number. all my attempts to do so actually failed with a pattern exception... except for this, a pattern like ```##0.00#######``` seems to work – spi Jan 15 '18 at 12:36

2 Answers2

1

You can try like something some this:

DecimalFormatSymbols formatSymbols = new 
DecimalFormatSymbols(Locale.getDefault());
formatSymbols.setDecimalSeparator(',');
formatSymbols.setGroupingSeparator(' ');
String pattern = "####,####.#####";
DecimalFormat decimalFormat = new DecimalFormat(pattern, formatSymbols);
decimalFormat.setGroupingSize(3);
String number = decimalFormat.format(178500.59999);
System.out.println(number);
0

Although this is a little hacky, you could extend the decimal format class this way:

    DecimalFormatSymbols formatSymbols = new
            DecimalFormatSymbols(Locale.getDefault());
    formatSymbols.setDecimalSeparator(',');
    formatSymbols.setGroupingSeparator(' ');
    final String pattern = "####,####.#####";
    DecimalFormat decimalFormat = new DecimalFormat(pattern, formatSymbols) {
        @Override
        public StringBuffer format(double number, StringBuffer res, FieldPosition fieldPosition) {
            StringBuffer noSpaceOnDecimalPart = super.format(number, res, fieldPosition);

            String result = noSpaceOnDecimalPart.toString();

            if (noSpaceOnDecimalPart != null) {
                int indexOf = noSpaceOnDecimalPart.indexOf(String.valueOf(getDecimalFormatSymbols().getDecimalSeparator()));
                if (indexOf >= 0) {
                    String integerPart = noSpaceOnDecimalPart.substring(0, indexOf);
                    String decimalPart = noSpaceOnDecimalPart.substring(indexOf + 1);

                    StringBuilder formattedDecimal = new StringBuilder("");

                    int i = 0;
                    while (i < decimalPart.length()) {
                        if (i != 0 && i % 3 == 0) {
                            formattedDecimal.append(getDecimalFormatSymbols().getGroupingSeparator());
                        }
                        formattedDecimal.append(decimalPart.charAt(i));
                        i++;
                    }

                    result = integerPart + getDecimalFormatSymbols().getDecimalSeparator() + formattedDecimal;
                }
            }

            return new StringBuffer(result);
        }
    };
    decimalFormat.setGroupingSize(3);
    String number = decimalFormat.format(178500.59999);
    System.out.println(number); // prints 178 500,599 99

This is just a demo, depending on your use case you could optimize the code and add support for more config (respect the contract of DecimalFormat better) but this is at least a start point. I didn't find any way to add spaces to the decimal part with a more conventional code.

spi
  • 1,673
  • 13
  • 19