Here you can find the source of magicRound(BigDecimal value)
public static BigDecimal magicRound(BigDecimal value)
//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; } } }