001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.math.BigDecimal;
018    import java.math.RoundingMode;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     * @author Wesley Gong
023     * @author Calvin Keum
024     */
025    public class BigDecimalUtil {
026    
027            public static double add(Number x, Number y) {
028                    if (x == null) {
029                            x = 0;
030                    }
031    
032                    if (y == null) {
033                            y = 0;
034                    }
035    
036                    BigDecimal xBigDecimal = new BigDecimal(x.toString());
037                    BigDecimal yBigDecimal = new BigDecimal(y.toString());
038    
039                    BigDecimal resultBigDecimal = xBigDecimal.add(yBigDecimal);
040    
041                    return resultBigDecimal.doubleValue();
042            }
043    
044            public static double divide(
045                    Number x, Number y, int scale, RoundingMode roundingMode) {
046    
047                    if (x == null) {
048                            x = 0;
049                    }
050    
051                    if (y == null) {
052                            y = 0;
053                    }
054    
055                    BigDecimal xBigDecimal = new BigDecimal(x.toString());
056                    BigDecimal yBigDecimal = new BigDecimal(y.toString());
057    
058                    BigDecimal resultBigDecimal = xBigDecimal.divide(
059                            yBigDecimal, scale, roundingMode);
060    
061                    return resultBigDecimal.doubleValue();
062            }
063    
064            public static double multiply(Number x, Number y) {
065                    if (x == null) {
066                            x = 0;
067                    }
068    
069                    if (y == null) {
070                            y = 0;
071                    }
072    
073                    BigDecimal xBigDecimal = new BigDecimal(x.toString());
074                    BigDecimal yBigDecimal = new BigDecimal(y.toString());
075    
076                    BigDecimal resultBigDecimal = xBigDecimal.multiply(yBigDecimal);
077    
078                    return resultBigDecimal.doubleValue();
079            }
080    
081            public static double scale(Number x, int scale, RoundingMode roundingMode) {
082                    if (x == null) {
083                            x = 0;
084                    }
085    
086                    BigDecimal xBigDecimal = new BigDecimal(x.toString());
087    
088                    xBigDecimal = xBigDecimal.setScale(scale, roundingMode);
089    
090                    return xBigDecimal.doubleValue();
091            }
092    
093            public static double subtract(Number x, Number y) {
094                    if (x == null) {
095                            x = 0;
096                    }
097    
098                    if (y == null) {
099                            y = 0;
100                    }
101    
102                    BigDecimal xBigDecimal = new BigDecimal(x.toString());
103                    BigDecimal yBigDecimal = new BigDecimal(y.toString());
104    
105                    BigDecimal resultBigDecimal = xBigDecimal.subtract(yBigDecimal);
106    
107                    return resultBigDecimal.doubleValue();
108            }
109    
110    }