Here you can find the source of roundToMSDecimal(BigDecimal sourceDecimal)
public static BigDecimal roundToMSDecimal(BigDecimal sourceDecimal)
//package com.java2s; /* Copyright (C) 2016 Advanced Digital Science Centre * This file is part of Soft-Grid./* www .j a v a2 s . co m*/ * For more information visit https://www.illinois.adsc.com.sg/cybersage/ * * Soft-Grid is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Soft-Grid is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Soft-Grid. If not, see <http://www.gnu.org/licenses/>. * @author Prageeth Mahendra Gunathilaka */ import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; public class Main { private static final BigDecimal LARGEST_DECIMAL = new BigDecimal( new BigInteger("ffffffffffffffffffffffff", 16)); private static final BigDecimal SMALLEST_DECIMAL = new BigDecimal( (new BigInteger("ffffffffffffffffffffffff", 16)).negate()); public static BigDecimal roundToMSDecimal(BigDecimal sourceDecimal) { BigInteger sourceDecimalIntComponent = sourceDecimal .unscaledValue(); BigDecimal destinationDecimal = new BigDecimal( sourceDecimalIntComponent, sourceDecimal.scale()); byte roundingModel = 4; validateDecimalMinMax(destinationDecimal); BigInteger allWordBigInt = destinationDecimal.unscaledValue(); if (allWordBigInt.bitLength() > 96) { destinationDecimal = destinationDecimal.round(new MathContext( 29)); if (allWordBigInt.bitLength() > 96) { destinationDecimal = destinationDecimal .round(new MathContext(28)); } } if (destinationDecimal.scale() > 28) { destinationDecimal = destinationDecimal.setScale(28, roundingModel); } if (destinationDecimal.scale() < 0) { destinationDecimal = destinationDecimal.setScale(0, roundingModel); } return destinationDecimal; } protected static void validateDecimalMinMax(BigDecimal in) { if (in == null) { throw new IllegalArgumentException( "null is not a supported Decimal value."); } else if (LARGEST_DECIMAL.compareTo(in) < 0) { throw new IllegalArgumentException( "Value too large for VT_DECIMAL data type:" + in.toString() + " integer: " + in.toBigInteger().toString(16) + " scale: " + in.scale()); } else if (SMALLEST_DECIMAL.compareTo(in) > 0) { throw new IllegalArgumentException( "Value too small for VT_DECIMAL data type:" + in.toString() + " integer: " + in.toBigInteger().toString(16) + " scale: " + in.scale()); } } }