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

com.ibm.commons.util.FormatUtil Maven / Gradle / Ivy

The newest version!
/*
 * © Copyright IBM Corp. 2012-2013
 * 
 * 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, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package com.ibm.commons.util;

import java.text.*;
import java.util.*;

/**
 * Default formatter objects.
 * This class simply exports some common default values that can be used by another classes.
 *
 * A lot of methods in this class are published in I18n class : look at I18n for more
 * detailed documentation.
 * 
 * @ibm-not-published
 * @deprecated
 */
public final class FormatUtil {
    
    private static final String INVALID_DATE = "Invalid date '{0}'"; // $NLS-FormatUtil.Invaliddate0-1$
    
    /* -------------------------- NUMBERS ----------------------------------------- */

    // Getting an Integer from a String (no locale / no tolerance)
    // only strings like '30000' are ok.
    public static long getInteger( String integerString) {
        if(StringUtil.isEmpty(integerString) ) {
            return 0;
        }
        return Long.parseLong(integerString);
    }

    // Getting an Integer from a String (with locale)
    // localized strings are accepted (ex : 30 000 in france)
    public static long parseInt(String src, Locale loc) throws ParseException{
        if (loc==null) {
            loc = Locale.getDefault();
        }
        NumberFormat nf = NumberFormat.getNumberInstance(loc);
        nf.setParseIntegerOnly(true);
        return nf.parse(src).longValue();
    }

    // Getting a Decimal from a String (no locale / no tolerance)
    public static double getDecimal(String doubleString) {
        if(StringUtil.isEmpty(doubleString) ) {
            return 0;
        }
        return Double.parseDouble(doubleString);
    }
    // Getting a Decimal from a String (accept comma or point in some locales)
    public static double parseFloatWithDecimalSeparatorTolerance(String doubleString, Locale loc) {
        doubleString = removeThousandSeparator(doubleString, loc);
        doubleString = replaceCommaByPoint(doubleString, loc);
        return getDecimal(doubleString);
    }

    /**
     * If a decimal number contains a comma and no point, and if the given
     * locale uses a comma for decimal separator, the comma is replaced by
     * a point.
     */
    public static String replaceCommaByPoint(String doubleString, Locale loc) {
        DecimalFormatSymbols symb = new DecimalFormatSymbols(loc);
        if (symb.getDecimalSeparator()==',') {
            // comma just 1 time, and no point
            if (   doubleString.indexOf(',')>=0
                && doubleString.indexOf(',')==doubleString.lastIndexOf(',')
                && doubleString.indexOf('.')==-1 ) {
                return doubleString.replace(',','.');
            }
        }
        return doubleString;
    }

    /**
     * If a decimal number contains thousand separator (example : a coma in US),
     * they are removed, so that the string can be correctly interpreted by Double.parseDouble
     */
    public static String removeThousandSeparator(String doubleString, Locale loc) {
        DecimalFormatSymbols symb = new DecimalFormatSymbols(loc);
        char thoSep = symb.getGroupingSeparator();
        if (thoSep=='.') { // don't remove thousand separator if it is a '.'
            return doubleString;
        }
        if (thoSep==160) { // insecable white space
            thoSep=' ';
        }
        return StringUtil.replace(doubleString, String.valueOf(thoSep), null);
    }


    // Getting a Decimal from a String (with locale)
    public static double parseFloat(String src, Locale loc) throws ParseException{
        if (loc==null) {
            loc = Locale.getDefault();
        }
        return NumberFormat.getInstance(loc).parse(src).floatValue();
    }



    // Formatting an integer
    public static String formatInteger( long l, String format, String emptyFormat ) {
        return formatInteger(l, format, emptyFormat, Locale.getDefault());
    }
    // Formatting an integer
    public static String formatInteger( long l, String format, String emptyFormat, Locale loc ) {
        if (loc==null) {
            loc = Locale.getDefault();
        }
        if( l==0.0 && !StringUtil.isEmpty(emptyFormat) ) {
            return emptyFormat;
        }
        NumberFormat fmt=null;
        if (!StringUtil.isEmpty(format)){
            fmt = new DecimalFormat(format, new DecimalFormatSymbols(loc));
        } else {
            fmt = NumberFormat.getNumberInstance(loc);
        }
        if (fmt!=null) {
            return fmt.format(l);
        } else {
            //should never append
            return Long.toString(l);
        }
    }


    // Formatting a decimal
    public static String formatDecimal( double d, String format, String emptyFormat ) {
        return formatDecimal(d, format, emptyFormat, Locale.getDefault());
    }
    // Formatting a decimal
    public static String formatDecimal(double d, String format, String emptyFormat, Locale loc) {
        if (loc==null) {
            loc = Locale.getDefault();
        }
        if( d==0.0 && !StringUtil.isEmpty(emptyFormat) ) {
            return emptyFormat;
        }
        NumberFormat fmt=null;
        if (!StringUtil.isEmpty(format)){
            fmt = new DecimalFormat(format, new DecimalFormatSymbols(loc));
        } else {
            fmt = NumberFormat.getNumberInstance(loc);
        }
        if (fmt!=null) {
            return fmt.format(d);
        } else {
            //should never append
            return Double.toString(d);
        }
    }



    /* -------------------------- DATE ----------------------------------------- */

    public static final String SHORT_DATE       = ""; //$NON-NLS-1$
    public static final String LONG_DATE        = ""; //$NON-NLS-1$
    public static final String SHORT_TIME       = "




© 2015 - 2024 Weber Informatics LLC | Privacy Policy