Guest User

Untitled

a guest
Dec 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. Examples 1:
  2. Value = 24,519.30235
  3. Significance = 0.01
  4. Returned Value = 24,519.30
  5.  
  6. Example 2:
  7. Value = 76.81485697
  8. Significance = 1
  9. Returned Value = 76
  10.  
  11. Example 3:
  12. Value = 12,457,854
  13. Significance = 100
  14. Returned Value = 12,457,800
  15.  
  16. round(200.3456, 2);
  17.  
  18. public static void main(String[] args) {
  19. BigDecimal value = new BigDecimal("2.0");
  20. BigDecimal significance = new BigDecimal("0.5");
  21. for (int i = 1; i <= 10; i++) {
  22. System.out.println(value + " --> " + floor(value, significance));
  23. value = value.add(new BigDecimal("0.1"));
  24. }
  25. }
  26.  
  27. private static double floor(BigDecimal value, BigDecimal significance) {
  28. double result = 0;
  29. if (value != null) {
  30. result = value.divide(significance).doubleValue();
  31. result = Math.floor(result) * significance.doubleValue();
  32. }
  33. return result;
  34. }
  35.  
  36. value
  37. .divide(significance)
  38. .setScale(0, RoundingMode.FLOOR)
  39. .multiply(significance);
Add Comment
Please, Sign In to add comment