85

I have a BigDecimal number and i consider only 2 decimal places of it so i truncate it using:

bd = bd.setScale(2, BigDecimal.ROUND_DOWN)

Now I want to print it as String but removing the decimal part if it is 0, for example:

1.00 -> 1

1.50 -> 1.5

1.99 -> 1.99

I tried using a Formatter, formatter.format but i always get the 2 decimal digits.

How can I do this? Maybe working on the string from bd.toPlainString()?

Jonik
  • 77,494
  • 68
  • 254
  • 365
res1
  • 3,124
  • 5
  • 27
  • 43

5 Answers5

111

I used DecimalFormat for formatting the BigDecimal instead of formatting the String, seems no problems with it.

The code is something like this:

bd = bd.setScale(2, BigDecimal.ROUND_DOWN);

DecimalFormat df = new DecimalFormat();
            
df.setMaximumFractionDigits(2);
            
df.setMinimumFractionDigits(0);
            
df.setGroupingUsed(false);

String result = df.format(bd);
res1
  • 3,124
  • 5
  • 27
  • 43
  • What is the expected output for this program. I got the same requirements to trailing zeros after decimal pointbut trailing zeros like **.00** truncating in output. – Paramesh Korrakuti May 29 '15 at 13:17
  • 1
    @ParameshKorrakuti `df.setMinimumFractionDigits(2)` works fine without the setScale and setGrouping and setMaxFrac. – Andrew Grinder Aug 06 '15 at 14:58
  • 4
    Decimal Format is NumberFormat in disguise. Use `NumberFormat numberFormat = NumberFormat.getPercentInstance();` then `numberFormat.setMinimumFractionDigits(2);` then `String percent = numberFormat.format(yourBigDecimal);` – Andrew Grinder Aug 06 '15 at 15:02
  • 1
    Java 9 and up `BigDecimal.ROUND_DOWN` is deprecated . Use `RoundingMode.DOWN` instead. – Dilantha Feb 25 '20 at 03:41
71
new DecimalFormat("#0.##").format(bd)
  • 45
    I use `new DecimalFormat("0.00")` if I want to ensure that the two decimal places are always shown, e.g. `1000.5` will display as `1000.50`. – Chris Parton Mar 12 '13 at 04:07
  • 1
    @ChrisParton's solution works perfectly, in addition to that if some one require to apply localization, we can use the below logic. `import java.math.BigDecimal; import java.text.*; public class LocalizeExample { public static void main(String[] args) { BigDecimal bd = new BigDecimal("123.10"); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(Locale.GERMAN); decimalFormat.applyPattern("#0.00"); String result = decimalFormat.format(bd); System.out.println(result); } }` – Paramesh Korrakuti May 29 '15 at 13:38
16

The below code may help you.

protected String getLocalizedBigDecimalValue(BigDecimal input, Locale locale) {
    final NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
    numberFormat.setGroupingUsed(true);
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMinimumFractionDigits(2);
    return numberFormat.format(input);
}
Paramesh Korrakuti
  • 1,845
  • 4
  • 23
  • 35
12

Use stripTrailingZeros().

This article should help you.

lrAndroid
  • 2,824
  • 17
  • 27
3

If its money use:

NumberFormat.getNumberInstance(java.util.Locale.US).format(bd)
Heisenberg
  • 5,294
  • 2
  • 28
  • 40