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. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static double round(double d, int n)
  11. {
  12. return ((int)(d * Math.pow(10.0, (double)(n)) + 0.5)) / Math.pow(10.0, (double)(n));
  13. }
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. double d = 12.34567890123;
  17. d = round(d, 1);
  18. System.out.println(d);
  19. d = 12.34567890123;
  20. d = round(d, 2);
  21. System.out.println(d);
  22. d = 12.34567890123;
  23. d = round(d, 3);
  24. System.out.println(d);
  25. d = 12.34567890123;
  26. d = round(d, 4);
  27. System.out.println(d);
  28. d = 12.34567890123;
  29. d = round(d, 5);
  30. System.out.println(d);
  31. }
  32. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
12.3
12.35
12.346
12.3457
12.34568