Java Locale Format format(Object input, int style)

Here you can find the source of format(Object input, int style)

Description

DOC Zqin Comment method "format".

License

Open Source License

Parameter

Parameter Description
input the object that was formated.
style the style of formated, it should be 0, 1,2,99999.

Return

the formated object.

Declaration

public static Object format(Object input, int style) 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.math.BigDecimal;
import java.text.DecimalFormat;

import java.util.Locale;

public class Main {
    public static final int PERCENT = 0;
    public static final int INT_NUMBER = 1;
    public static final int DOUBLE_NUMBER = 2;

    /**/*from   ww w .  ja  v  a 2  s  . c o m*/
     * DOC Zqin Comment method "format".
     * 
     * @param input the object that was formated.
     * @param style the style of formated, it should be 0, 1,2,99999.
     * @return the formated object.
     */
    public static Object format(Object input, int style) {

        try {
            if (checkInput(input)) {
                DecimalFormat format = null;

                // MOD qiongli 2011-6-1 bug 21589,if input is out of the decimal precision,replace it with threshold.
                BigDecimal zero = new BigDecimal(0);
                BigDecimal temp = new BigDecimal(input.toString());
                BigDecimal min = new BigDecimal(10E-5);
                BigDecimal max = new BigDecimal(9999 * 10E-5);
                boolean isUseScientific = false;
                switch (style) {
                case PERCENT:
                    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
                        isUseScientific = true;
                    } else if (temp.compareTo(max) == 1 && temp.compareTo(new BigDecimal(1)) == -1) {
                        input = max.toString();
                    }
                    format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH);
                    format.applyPattern("0.00%"); //$NON-NLS-1$
                    break;
                case INT_NUMBER:
                    min = new BigDecimal(10E-3);
                    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
                        isUseScientific = true;
                    }
                    format = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.ENGLISH);
                    format.applyPattern("0"); //$NON-NLS-1$
                    break;
                case DOUBLE_NUMBER:
                    min = new BigDecimal(10E-3);
                    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
                        isUseScientific = true;
                    }
                    format = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.ENGLISH);
                    format.applyPattern("0.00"); //$NON-NLS-1$
                    break;
                default:
                    format = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
                    return format.parse(input.toString());
                }
                if (isUseScientific) {
                    format.applyPattern("0.###E0%"); //$NON-NLS-1$
                }

                return format.format(new Double(input.toString()));
            } else {
                return input;
            }
        } catch (Exception e) {
            return input;
        }
    }

    /**
     * DOC Zqin Comment method "checkInput".
     * 
     * @param input the object that was formated.
     * @return true if the input is valid, else false;
     */
    private static boolean checkInput(Object input) {
        if (input == null || "".equals(input)) { //$NON-NLS-1$
            return false;
        } else {
            Double db = new Double(input.toString());
            if (db.equals(Double.NaN)) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. format(int[] a)
  2. format(Locale locale, ResourceBundle bundle, String key, Object... args)
  3. format(Locale locale, String key, Object... arguments)
  4. format(Number number)
  5. format(Number number, Locale locale)
  6. format(String bundleName, Locale locale, String key, Object arg1)
  7. format(String format, Date date, Locale locale)
  8. format(String message, Object[] params)
  9. format(String pattern, Date date)