0

I want to convert an incoming Double to String, where there is a thousand seperator.

For example 159727,5 should be 159 727,5

Here is my code:

public static String formatToHungraianNumber(Double toFormat) {
    logger.info("formatToHungraianNumber begin :" + toFormat);
    String formatted = toFormat.toString();
    try {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setGroupingSeparator(' ');
        DecimalFormat format = new DecimalFormat("# ###,00", symbols);
        formatted = String.format("%14s", format.format(toFormat));
    } catch (Exception e) {
        logger.info("Runtime Exception", e);
        return formatted;
    }
    logger.info("Returned:" + formatted);
    return formatted;
}

Instead of return 159 727,5 it return 15 97 28 I had seen this question, but I DON't want my result to be Truncated. What is wrong with my code?

Community
  • 1
  • 1
Gábor Csikós
  • 2,787
  • 7
  • 31
  • 57

1 Answers1

1
DecimalFormat format = new DecimalFormat("# ###,00", symbols);

should be:

DecimalFormat format = new DecimalFormat("#,###.00", symbols);

The syntax of DecimalFormat doesn't change. Only the output changes.

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50