All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.util.BigDecimalHelper Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2008 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

package at.spardat.enterprise.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import at.spardat.enterprise.exc.SysException;

/**
 *
 * Helper class to deal with change of behauvior of BigDecimal from Java version 1.4.2 to 5.0.
 * Till Java version 1.4.2 BigDecimal.toString() returned a string representation of this BigDecimal without an exponent field.
 * Since Java version 5.0 BigDecimal.toString() can return a value using scientific notation with an exponent.
 * In Java 5.0 BigDecimal.toPlainString() was added, this method has still the old behauvior.
 * As the epclient project expects the old behauvior, we have to call the right method depending at the JRE version and return value.
 * This helper class ensures that the value is always returned in the 1.4.2 BigDecimal.toString() notation.
 *
 * As soon as openXMA has not to support JRE 1.4.2 anymore this class can be removed!
 * Under JDK 5.0 BigDecimal.toPlainString() can be used instead directly.
 *
 * For details about the BigDecimal change see:
 * http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#toString()
 * http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#toPlainString()
 * http://blogs.sun.com/darcy/entry/compatibly_evolving_bigdecimal
 *
 *
 * @author webok
 * @since 5.0.4
 */
public class BigDecimalHelper {

    private static boolean isNoJRE142 = false;

    static{
        isNoJRE142 = !System.getProperty("java.vm.version").startsWith("1.4.2");
    }

    /**
     *
     * @param bigDec its toString() value in the Java 1.4.2 style will be returned
     * @return the value of 'bigDec.toString()'. If running at a JRE > 1.4.2 and in the case that the value of 'bigDec.toString()' returns an exponential notation then the value 'bigDec.toPlainString()' is returned. BigDecimal.toPlainString() was added with JDK 5.0
     * @since version_number
     * @author s3460
     */
    public static String toPlainString(BigDecimal bigDec){

        String value = bigDec.toString();

        if(isNoJRE142 && value.indexOf('E')>=0){
            try {
                Method meth = bigDec.getClass().getMethod("toPlainString", null);
                value = (String) meth.invoke(bigDec, null);
            } catch (Exception e) {
                throw new SysException(e);
            }
        }

        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy