fork(1) 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. double[]n = {
  14. 0.34567089445515,
  15. 0.034567089445515,
  16. 0.0034567089445515,
  17. 0.00034567089445515,
  18. 0.000034567089445515,
  19. 0.0000034567089445515,
  20. 0.00000034567089445515,
  21. 0.000000034567089445515,
  22. 0.0000000034567089445515,
  23. 0.00000000034567089445515
  24. };
  25.  
  26. for(int i=0; i<n.length; i++) {
  27. System.out.print((i+1) + ": ");
  28. System.out.println(truncate(n[i]).toPlainString());
  29. }
  30. }
  31.  
  32.  
  33. // turned to bigdecimal
  34. static BigDecimal truncate(double value) {
  35. int precision = 10;
  36. // removed to show behaviour
  37. /*if (value != 0 && Math.abs(value) < 1) {
  38. precision--;
  39. }*/
  40.  
  41. BigDecimal b = new BigDecimal(String.valueOf(value));
  42. b = b.round(new MathContext(precision));
  43. return b;
  44. }
  45. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
1: 0.3456708945
2: 0.03456708945
3: 0.003456708945
4: 0.0003456708945
5: 0.00003456708945
6: 0.000003456708945
7: 0.0000003456708945
8: 0.00000003456708945
9: 0.000000003456708945
10: 0.0000000003456708945