Java BigDecimal Round magicRound(BigDecimal value)

Here you can find the source of magicRound(BigDecimal value)

Description

magic Round

License

Open Source License

Declaration

public static BigDecimal magicRound(BigDecimal value) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;

public class Main {
    private final static BigDecimal ONE = BigDecimal.valueOf(1.0);
    private final static BigDecimal MINUS_ONE = BigDecimal.valueOf(-1.0);

    public static double magicRound(double value) {
        if (value > -1.0 && value < 1.0) {
            return new BigDecimal(value).setScale(2 - (int) Math.log10(Math.abs(value)), BigDecimal.ROUND_HALF_UP)
                    .doubleValue();/* w w w . j  a  va 2s  . c o m*/
        } else {
            return value;
        }
    }

    public static BigDecimal magicRound(BigDecimal value) {
        if (value.compareTo(MINUS_ONE) > 0 && value.compareTo(ONE) < 0) {
            return value.setScale(2 - (int) Math.log10(Math.abs(value.doubleValue())), BigDecimal.ROUND_HALF_UP);
        } else {
            return value;
        }
    }
}

Related

  1. getEfficientRound(BigDecimal val, int effectiveDigit)
  2. getRoundedAmt(BigDecimal amtBeforeRnd)
  3. getRoundedBigDecimal(double value)
  4. getRoundedBigDecimal(double value, int scale)
  5. getStringValue(BigDecimal val, int newScale, int roundingMode)
  6. round(BigDecimal amount)
  7. round(BigDecimal aValue, int aScale)
  8. round(BigDecimal b, int precision)
  9. round(BigDecimal d, int decimalDigits, RoundingMode rmode)