92

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
  • 80,077
  • 70
  • 264
  • 372
res1
  • 3,482
  • 5
  • 29
  • 50

5 Answers5

118

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,482
  • 5
  • 29
  • 50
  • 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
  • 2
    Java 9 and up `BigDecimal.ROUND_DOWN` is deprecated . Use `RoundingMode.DOWN` instead. – Dilantha Feb 25 '20 at 03:41
81
new DecimalFormat("#0.##").format(bd)
  • 48
    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
  • 2
    @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,997
  • 4
  • 27
  • 39
12

Use stripTrailingZeros().

This article should help you.

lrAndroid
  • 2,834
  • 18
  • 27
4

If its money use:

NumberFormat.getNumberInstance(java.util.Locale.US).format(bd)
Heisenberg
  • 5,514
  • 2
  • 32
  • 43