-1
import java.math.BigDecimal; 
import java.text.DecimalFormat;

public class HelloWorld{

     public static void main(String []args){
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("two  decimals: " + new BigDecimal(df.format(123.435435))) ;
        System.out.println("zero two decimal places: " + new BigDecimal(df.format(0.0000))) ;
        System.out.println("zero without a decimal place: " + new BigDecimal(df.format(0))) ;
     }
}

The output is:

three decimals: 123.44
zero two decimal places: 0
zero without a decimal place: 0

Now, DecimalFormat df = new DecimalFormat("#.##");

This is something that is used widely in my application and I do not, yet, have an option to modify this. How can I print 0 as 0.00 in the above program?

user207421
  • 305,947
  • 44
  • 307
  • 483
Sara
  • 603
  • 8
  • 19
  • Decimal *places*, not 'points'. Same would happen for `123.00`. Try `"#.00"`. `#` basically means 'optional'. – user207421 Mar 28 '20 at 05:45

2 Answers2

1

According to the Javadoc formatting with '#' will show zero as absent. You can use '0' instead to show a decimal place unconditionally, even if the digit concerned is zero.

Note that BigDecimals are used for calculations where high precision is required, e.g. financial applications. If you just need to print formatted numbers, you don't really need them.

  DecimalFormat df = new DecimalFormat("#.##");
  DecimalFormat dfWithZeroes = new DecimalFormat("0.00");
  System.out.println("two  decimals: " + df.format(123.435435));
  System.out.println("zero two decimals: " + dfWithZeroes.format(0.0000));
  System.out.println("zero without a decimal: " + df.format(0));

With BigDecimals:

  System.out.println("two  decimals: " + new BigDecimal(df.format(123.435435)));
  System.out.println("zero two decimals: " + new BigDecimal(dfWithZeroes.format(0.0000)));
  System.out.println("zero without a decimal: " + new BigDecimal(df.format(0)));
Monopole Magnet
  • 435
  • 5
  • 19
  • Yes. The application needs high precision numbers. Hence, looking for a solution. – Sara Mar 28 '20 at 06:42
  • I added an example with BigDecimals. – Monopole Magnet Mar 28 '20 at 07:35
  • New examples are fairly pointless. If you already have the values you wouldn't truncate them via `DecimalFormst`, you would use the API of `BigDecimal`. – user207421 Mar 28 '20 at 07:48
  • @user207421 Pretty much true. I used scale method and it worked as per my expectations. Posted my new code as an answer. Thanks for the tip. – Sara Mar 28 '20 at 13:05
0
import java.math.BigDecimal; 
import java.text.DecimalFormat;

public class HelloWorld{

public static void main(String []args){
  DecimalFormat df = new DecimalFormat("#.##");

  System.out.println("");
  System.out.println("------------Handling zero with Decimal Formatter API------------");
  System.out.println("");


  DecimalFormat dfWithZeroes = new DecimalFormat("0.0");
  System.out.println("two  decimals: " + df.format(123.435435));
  System.out.println("zero two decimals: " + dfWithZeroes.format(0));
  System.out.println("zero without a decimal: " + df.format(0));

  System.out.println("");
  System.out.println("------------Handling zero with BigDecimal API------------");
  System.out.println("");

  BigDecimal zero = BigDecimal.ZERO;
  System.out.println("Zero without scaling " + zero);
  System.out.println("Zero with scale 1: " + zero.setScale(1));
  System.out.println("Zero with scale 2: " + zero.setScale(2));
  System.out.println("Zero with scale 3: " + zero.setScale(3));
 }
}
Sara
  • 603
  • 8
  • 19