home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (BigDecimalMath.java)

This example Groovy source code file (BigDecimalMath.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

arithmeticexception, bigdecimal, bigdecimal, bigdecimalmath, bigdecimalmath, division_extra_precision, division_extra_precision, division_min_scale, instance, math, mathcontext, number, number, numbermath

The Groovy BigDecimalMath.java source code

 * Copyright 2003-2010 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.codehaus.groovy.runtime.typehandling;

import java.math.BigDecimal;
import java.math.MathContext;

 * BigDecimal NumberMath operations
 * @author Steve Goetze
public final class BigDecimalMath extends NumberMath {

    // This is an arbitrary value, picked as a reasonable choice for a precision
    // for typical user math when a non-terminating result would otherwise occur.
    public static final int DIVISION_EXTRA_PRECISION = 10;

    //This is an arbitrary value, picked as a reasonable choice for a rounding point
    //for typical user math.
    public static final int DIVISION_MIN_SCALE = 10;

    public static final BigDecimalMath INSTANCE = new BigDecimalMath();

    private BigDecimalMath() {

    protected Number absImpl(Number number) {
        return toBigDecimal(number).abs();

    public Number addImpl(Number left, Number right) {
        return toBigDecimal(left).add(toBigDecimal(right));

    public Number subtractImpl(Number left, Number right) {
        return toBigDecimal(left).subtract(toBigDecimal(right));

    public Number multiplyImpl(Number left, Number right) {
        return toBigDecimal(left).multiply(toBigDecimal(right));

    public Number divideImpl(Number left, Number right) {
        BigDecimal bigLeft = toBigDecimal(left);
        BigDecimal bigRight = toBigDecimal(right);
        try {
            return bigLeft.divide(bigRight);
        } catch (ArithmeticException e) {
            // set a DEFAULT precision if otherwise non-terminating
            int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
            BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
            int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
            if (result.scale() > scale) result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
            return result;

    public int compareToImpl(Number left, Number right) {
        return toBigDecimal(left).compareTo(toBigDecimal(right));

    protected Number unaryMinusImpl(Number left) {
        return toBigDecimal(left).negate();

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy BigDecimalMath.java source code file:

new blog posts


Copyright 1998-2013 Alvin Alexander, alvinalexander.com
All Rights Reserved.