Show cents only if price contains them I have to show price of an item. If the price is only in dollars, it should look like an integer. But if it contains cents, it should look like double value and always have two symbols after decimal separator:


  2 550$
  2 550,55$
  2 550,50$


I decided to use Decimal format for this task, but I don't know how to properly handle the cents.

Here is my solution:

public static String moneyFormat(double amount, String currency) {
    DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
    dfSymbols.setDecimalSeparator(',');
    dfSymbols.setGroupingSeparator(' ');
    dfSymbols.setCurrencySymbol(currency);
    DecimalFormat df;
    if (isInteger(amount)) {
        df = new DecimalFormat("###,###,##0 \u00A4", dfSymbols);
    } else {
        df = new DecimalFormat("###,###,##0.00 \u00A4", dfSymbols);
    }
    df.setGroupingUsed(true);
    return df.format(amount);
}

private static boolean isInteger(double amount) {
    return  !Double.isInfinite(amount) &amp;&amp; (amount == Math.floor(amount));
}


The amount comes from server in Double, therefore, probably, we can have some errors in case if 2$ will be represented as 1,999(9)$ but I am not sure how to test for such errors.

As an alternative to passing two patterns I can use             df.setMaximumFractionDigits();:

 df = new DecimalFormat("###,###,##0.00 \u00A4", dfSymbols);
if (isInteger(amount)) {
    df.setMaximumFractionDigits(0);
} else {
    df.setMaximumFractionDigits(2);
}


But the question stays the same: can I handle integers in some other, cleaner way?

Update

Unfortunately, changing output from server to String or other types is not an option. And I forgot to mention, that it for Android. I don't use classes of platform, so I haven't added it to tags.
I know that you receive double, but you shouldn't get your system "infected" by double prices. This is just wrong. Convert them once you received them.

Instead of comparing your Doubles to find the Integer, you could also just work with the String and replace the zeros. This will also work with your double values and is a valid approach if you just want to render the number.

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Main
{
    public static void main(String... args)
    {
        System.out.println(format(new BigDecimal("2.1111")));
        System.out.println(format(new BigDecimal("5454542.1111")));
        System.out.println(format(new BigDecimal("5454542.1")));
        System.out.println(format(new BigDecimal("5454542")));
    }

    private static String format(BigDecimal price)
    {
        return createFormat().format(price).replace(",00", "");
    }

    private static DecimalFormat createFormat()
    {
        DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
        dfSymbols.setDecimalSeparator(',');
        dfSymbols.setGroupingSeparator(' ');
        final String currency = "$";
        dfSymbols.setCurrencySymbol(currency);
        return new DecimalFormat("###,###,##0.00 \u00A4", dfSymbols);
    }
}


This will give you:

2,11 $
5 454 542,11 $
5 454 542,10 $
5 454 542 $