Example usage for java.text NumberFormat getInstance

List of usage examples for java.text NumberFormat getInstance

Introduction

In this page you can find the example usage for java.text NumberFormat getInstance.

Prototype

public static final NumberFormat getInstance() 

Source Link

Document

Returns a general-purpose number format for the current default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

public static String getPercentage(Integer i, Integer total) {
    if (i == 0) {
        return "0%/";
    }/*from  w  w w  .  j  a v  a 2s  .c o  m*/
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setMaximumFractionDigits(1);
    return numberFormat.format((float) i / (float) total * 100) + "%/";
}

From source file:Main.java

public static String setThousandSeparator(long number) {
    BigDecimal bd = new BigDecimal(number);
    NumberFormat formatter = NumberFormat.getInstance();
    return formatter.format(bd.longValue());
}

From source file:Main.java

/**
 * /*  w ww .j a v  a 2 s .com*/
 * @param value
 * @param min_fraction_digit
 * @param max_fraction_digit
 * @return
 */
public static String formatNumber(double value, int min_fraction_digit, int max_fraction_digit) {
    NumberFormat nf;
    nf = NumberFormat.getInstance();

    if (min_fraction_digit != 0) {
        nf.setMinimumFractionDigits(min_fraction_digit);
    }

    if (max_fraction_digit != 0) {
        nf.setMaximumFractionDigits(max_fraction_digit);
    }

    return nf.format(value);
}

From source file:Main.java

/**
 * Converts a length of bytes to MB.//  w  ww .ja va2s. co m
 * 
 * @param bytes
 *            The bytes to convert
 * @return A string representation of the MB
 */
public static String convertBytesToMB(long bytes) {
    double result = (double) bytes / 1000 / 1000;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    nf.setGroupingUsed(false);
    return nf.format(result) + " MB";
}

From source file:Main.java

public static String getBalanceDisplay(String bal, int floatSize) {
    if ("".equals(bal)) {
        return "";
    } else {//from  w w  w. j  a  v a2 s  .c  om
        long amount = Long.parseLong(bal);
        NumberFormat format = NumberFormat.getInstance();
        if (NumEmpty == amount)
            return "";
        if (floatSize < 0) { // error input
            return Long.toString(amount);
        }
        if (floatSize == 2) {
            boolean zs = true;
            if (amount < 0) {
                zs = false;
                amount = -amount;
            }
            long m = amount / 100;
            long f = amount - m * 100;
            return (zs ? "" : "-") + format.format(m) + "." + (f > 9 ? Long.toString(f) : "0" + f);
        } else if (floatSize == 0) {
            return format.format(amount);
        } else { // error input
            return format.format(amount);
        }
    }
}

From source file:Main.java

public static double getAmount(String amountStr) {
    if (TextUtils.isEmpty(amountStr))
        return 0;
    else {//from   w  ww  .ja v  a 2  s .  co m
        if (amountStr.startsWith("+"))
            amountStr = amountStr.substring(1);
        try {
            return NumberFormat.getInstance().parse(amountStr).doubleValue();
        } catch (ParseException e) {
            return 0;
        }
    }
}

From source file:Main.java

public static String getTimeFromLong(long l) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(l);/*from   w  ww  .jav  a  2  s  .  c  o m*/

    Calendar t = Calendar.getInstance();
    t.set(Calendar.HOUR_OF_DAY, 0);
    t.set(Calendar.MINUTE, 1);
    t.set(Calendar.SECOND, 0);

    if (c.after(t)) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(2);

        return nf.format(c.get(Calendar.HOUR_OF_DAY)) + ":" + nf.format(c.get(Calendar.MINUTE));
    } else {
        return TIME_NONE;
    }
}

From source file:Main.java

private static Format createFormat() {
    NumberFormat format = NumberFormat.getInstance();
    format.setParseIntegerOnly(true);/*ww  w.  j ava  2 s . co m*/
    return new Format() {
        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            return format.format(obj, toAppendTo, pos);
        }

        @Override
        public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
            return format.formatToCharacterIterator(obj);
        }

        @Override
        public Object parseObject(String source, ParsePosition pos) {
            int initialIndex = pos.getIndex();
            Object result = format.parseObject(source, pos);
            if (result != null && pos.getIndex() > initialIndex + 1) {
                int errorIndex = initialIndex + 1;
                pos.setIndex(initialIndex);
                pos.setErrorIndex(errorIndex);
                return null;
            }
            return result;
        }
    };
}

From source file:Main.java

public static String formatNumber(int num) {
    return NumberFormat.getInstance().format(num);
}

From source file:com.norbl.util.StringUtil.java

public static String f1(double x) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(1);/*  w  ww .  ja  v  a 2s . c om*/
    f.setMinimumFractionDigits(1);
    return (f.format(x));
}