fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.*;
  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. // create 3 BigDecimal objects
  14. BigDecimal bg1, bg2, bg3;
  15.  
  16. MathContext mc = new MathContext(4); // 2 precision
  17.  
  18. bg1 = new BigDecimal("100.123");
  19. bg2 = new BigDecimal("50.56");
  20.  
  21. // subtract bg1 with bg2 using mc and assign result to bg3
  22. bg3 = bg1.subtract(bg2, mc);
  23.  
  24. String str = "The Result of Subtraction is " + bg3;
  25.  
  26. // print bg3 value
  27. System.out.println( str );
  28. }
  29. }
Success #stdin #stdout 0.12s 36480KB
stdin
Standard input is empty
stdout
The Result of Subtraction is 49.56