Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Examples 1:
- Value = 24,519.30235
- Significance = 0.01
- Returned Value = 24,519.30
- Example 2:
- Value = 76.81485697
- Significance = 1
- Returned Value = 76
- Example 3:
- Value = 12,457,854
- Significance = 100
- Returned Value = 12,457,800
- round(200.3456, 2);
- public static void main(String[] args) {
- BigDecimal value = new BigDecimal("2.0");
- BigDecimal significance = new BigDecimal("0.5");
- for (int i = 1; i <= 10; i++) {
- System.out.println(value + " --> " + floor(value, significance));
- value = value.add(new BigDecimal("0.1"));
- }
- }
- private static double floor(BigDecimal value, BigDecimal significance) {
- double result = 0;
- if (value != null) {
- result = value.divide(significance).doubleValue();
- result = Math.floor(result) * significance.doubleValue();
- }
- return result;
- }
- value
- .divide(significance)
- .setScale(0, RoundingMode.FLOOR)
- .multiply(significance);
Add Comment
Please, Sign In to add comment