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. /*
  14. 2,15 = 2,2
  15. 0,15 = 0,2
  16. 2,05 = 2,1
  17. 2,04 = 2,1
  18. 0,04 = 0,1*/
  19.  
  20. double v0 = 2.15;
  21. double v1 = 0.15;
  22. double v2 = 2.05;
  23. double v3 = 2.04;
  24. double v4 = 0.19;
  25. double v5 = 0.9;
  26.  
  27. System.out.println(prepareValue(v0));
  28. System.out.println(prepareValue(v1));
  29. System.out.println(prepareValue(v2));
  30. System.out.println(prepareValue(v3));
  31. System.out.println(prepareValue(v4));
  32. System.out.println(prepareValue(v5));
  33.  
  34. }
  35.  
  36. public static Object prepareValue(double value)
  37. {
  38. int r = 10;
  39. String[] str = String.valueOf(value).split("\\.");
  40. if (str.length > 0)
  41. {
  42. str[1] = str[1].length() == 1 ? (str[1]+"0") : str[1];
  43. int s1 = Integer.parseInt(str[1]);
  44. if (s1 < 10) r = 10; else
  45. if (s1 >= 10 && s1 <20) r = 20; else
  46. if (s1 >= 20 && s1 <30) r = 30; else
  47. if (s1 >= 30 && s1 <40) r = 40; else
  48. if (s1 >= 40 && s1 <50) r = 50; else
  49. if (s1 >= 50 && s1 <60) r = 60; else
  50. if (s1 >= 60 && s1 <70) r = 70; else
  51. if (s1 >= 70 && s1 <80) r = 80; else
  52. if (s1 >= 80 && s1 <90) r = 90; else
  53. if (s1 >= 90) r = 100;
  54. }
  55. BigDecimal re = new BigDecimal(Math.abs(((value - (int)value)*100)-r)/100)
  56. .setScale(2, RoundingMode.HALF_EVEN);
  57.  
  58. return re.add(new BigDecimal(value).setScale(2, RoundingMode.HALF_EVEN))
  59. .doubleValue();
  60. }
  61. }
Success #stdin #stdout 0.06s 4386816KB
stdin
Standard input is empty
stdout
2.2
0.2
2.1
2.1
0.2
1.0