code_junkie

Please explain the use of java.math.MathContext

Nov 10th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. System.out.println(new BigDecimal("123.4",
  2. new MathContext(4,RoundingMode.HALF_UP)));
  3. System.out.println(new BigDecimal("123.4",
  4. new MathContext(2,RoundingMode.HALF_UP)));
  5. System.out.println(new BigDecimal("123.4",
  6. new MathContext(2,RoundingMode.CEILING)));
  7. System.out.println(new BigDecimal("123.4",
  8. new MathContext(1,RoundingMode.CEILING)));
  9.  
  10. 123.4
  11. 1.2E+2
  12. 1.3E+2
  13. 2E+2
  14.  
  15. BigDecimal original = new BigDecimal("1.235");
  16. BigDecimal scaled = original.setScale(2, BigDecimal.ROUND_HALF_UP);
  17.  
  18. System.out.println(new BigDecimal("1234567890.123456789",
  19. new MathContext(20)));
  20.  
  21. System.out.println(new BigDecimal("1234567890.123456789",
  22. new MathContext(10)));
  23.  
  24. System.out.println(new BigDecimal("1234567890.123456789",
  25. new MathContext(5)));
  26.  
  27. 1234567890.123456789
  28. 1234567890
  29. 1.2346E+9
Add Comment
Please, Sign In to add comment