fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.math.* ;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. BigDecimal eightyPercent = new BigDecimal( "0.8" ) ;
  15. BigDecimal bd = new BigDecimal( "4096" ).multiply( eightyPercent ) ;
  16. BigDecimal rounded = bd.setScale( 0 , RoundingMode.HALF_EVEN ) ; // Bankers rounding.
  17. int result = rounded.intValueExact() ; // Throws exception if not fitting into an `int` with no data loss.
  18.  
  19. System.out.println( eightyPercent ) ;
  20. System.out.println( bd ) ;
  21. System.out.println( rounded ) ;
  22. System.out.println( result ) ;
  23.  
  24. System.out.println(
  25. new BigDecimal( "4096" )
  26. .multiply( new BigDecimal( "0.8" ) )
  27. .setScale( 0 , RoundingMode.HALF_EVEN )
  28. .intValueExact()
  29. ) ;
  30. }
  31. }
Success #stdin #stdout 0.08s 51716KB
stdin
Standard input is empty
stdout
0.8
3276.8
3277
3277
3277