fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.math.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. try {
  14. System.out.println(BigDecimal.valueOf(2).divide(BigDecimal.valueOf(3)).toPlainString());
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. try {
  19. System.out.println(BigDecimal.valueOf(2).divide(BigDecimal.valueOf(3), MathContext.DECIMAL128).toPlainString());
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
Success #stdin #stdout #stderr 0.07s 380224KB
stdin
Standard input is empty
stdout
0.6666666666666666666666666666666667
stderr
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
	at java.math.BigDecimal.divide(BigDecimal.java:1616)
	at Ideone.main(Main.java:14)