Frames | No Frames |
1: /** 2: * ========================================= 3: * LibFormula : a free Java formula library 4: * ========================================= 5: * 6: * Project Info: http://reporting.pentaho.org/libformula/ 7: * 8: * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * 26: * ------------ 27: * $Id: NumberUtil.java 3522 2007-10-16 10:56:57Z tmorgner $ 28: * ------------ 29: * (C) Copyright 2006-2007, by Pentaho Corporation. 30: */ 31: 32: package org.jfree.formula.util; 33: 34: import java.math.BigDecimal; 35: 36: import org.jfree.formula.EvaluationException; 37: import org.jfree.formula.LibFormulaErrorValue; 38: 39: public class NumberUtil 40: { 41: public static final BigDecimal DELTA = new BigDecimal("0.000000000000000000000000000005"); 42: 43: private NumberUtil() 44: { 45: } 46: 47: public static BigDecimal getAsBigDecimal(final Number number) throws EvaluationException 48: { 49: if (number == null) 50: { 51: throw new EvaluationException( 52: LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); 53: } 54: 55: if(number instanceof BigDecimal) 56: { 57: return (BigDecimal)number; 58: } 59: else 60: { 61: return new BigDecimal(number.toString()); 62: } 63: } 64: 65: public static Integer performIntRounding(BigDecimal n) 66: { 67: 68: try 69: { 70: // no need to go further if the value is already an integer 71: n = n.setScale(0); 72: return new Integer(n.intValue()); 73: } 74: catch(ArithmeticException e) 75: { 76: //ignore and continue 77: } 78: 79: final BigDecimal round; 80: if(n.signum()<0) 81: { 82: n = n.subtract(DELTA); 83: round = n.setScale(0, BigDecimal.ROUND_UP); 84: } 85: else 86: { 87: n = n.add(DELTA); 88: round = n.setScale(1, BigDecimal.ROUND_DOWN); 89: } 90: return new Integer(round.intValue()); 91: } 92: 93: public static BigDecimal removeTrailingZeros(BigDecimal bd) 94: { 95: if(bd.signum() == 0) 96: { 97: return bd.setScale(0); 98: } 99: 100: // Todo: This approach is very expensive. There must be a better way ... 101: try 102: { 103: while(true) 104: { 105: final int scale = bd.scale(); 106: if (scale == 0) 107: { 108: return bd; 109: } 110: bd = bd.setScale(scale-1); 111: } 112: } 113: catch(ArithmeticException ae) 114: { 115: return bd; 116: } 117: } 118: }