Java Number Format Pattern formatI18N(Object ob)

Here you can find the source of formatI18N(Object ob)

Description

format IN

License

Open Source License

Declaration

public static String formatI18N(Object ob) 

Method Source Code


//package com.java2s;
import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Main {

    public static String formatI18N(Object ob, String formatType) {
        if (ob == null) {
            return "";
        }//from  www  . j  av a 2 s . c  o m

        Double value = toDouble(ob, null);
        if (value == null) {
            return "";
        }

        //value = round(value,4);

        DecimalFormat sos = new DecimalFormat(formatType);
        String s = sos.format(value);
        //s = filterZero(s);

        //        String[] ss = s.split("\\.");
        //        if( ss.length==1 ){
        //            return s+".00";
        //        }
        //        
        //        if( ss[1].length()==1 ){
        //            return s+"0";
        //        }

        return s;
    }

    public static String formatI18N(Object ob) {
        if (ob == null) {
            return "";
        }

        Double value = toDouble(ob, null);
        if (value == null) {
            return "";
        }

        value = round(value, 4);

        DecimalFormat sos = new DecimalFormat("##,##0.0000");
        String s = sos.format(value);
        s = filterZero(s);

        String[] ss = s.split("\\.");
        if (ss.length == 1) {
            return s + ".00";
        }

        if (ss[1].length() == 1) {
            return s + "0";
        }

        return s;
    }

    public static Double toDouble(Object ob, Double defaultDouble) {

        if (ob == null) {
            return defaultDouble;
        }

        if (ob instanceof Integer) {
            return ((Integer) ob).doubleValue();
        } else if (ob instanceof Float) {
            return ((Float) ob).doubleValue();
        } else if (ob instanceof Double) {
            return ((Double) ob).doubleValue();
        } else if (ob instanceof Byte) {
            return ((Byte) ob).doubleValue();
        } else {
            try {
                return new Double(ob.toString());
            } catch (Exception e) {
                return defaultDouble;
            }
        }
    }

    public static Double toDouble(Object ob) {
        return toDouble(ob, 0d);
    }

    public static double round(double v, int scale) {
        if (scale < 0)
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, 4).doubleValue();
    }

    public static String filterZero(String str) {
        if (str == null || str.trim().length() == 0 || str.indexOf(".") == -1) {
            return str;
        }

        String[] s = str.split("\\.");
        if (s[1].length() <= 2) {
            return str;
        }
        while (s[1].endsWith("0")) {
            s[1] = s[1].substring(0, s[1].length() - 1);
        }

        if (s[1].length() == 0) {
            return s[0] + ".00";
        } else if (s[1].length() == 1) {
            return s[0] + "." + s[1] + "0";
        } else {
            return s[0] + "." + s[1];
        }
    }
}

Related

  1. formatDisplay(String displayString)
  2. formatDollar(Object obj)
  3. formatDollarTd(Object obj)
  4. formatFileLength(long length)
  5. formatGopNumber(Number gop)
  6. formatiereSpeichergroesse(long bytes)
  7. formatIncludeCommas(final Number object)
  8. formatInt(final int number)
  9. formatInteger(Object obj)