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 NumberFormat getInstance(Locale inLocale) 

Source Link

Document

Returns a general-purpose number format for the specified locale.

Usage

From source file:Main.java

/**
 * Str format.//from w  w  w  .ja va2s .c  om
 * 
 * @param v
 *            the v
 * @return the string
 */
public static String strFormat(double v) {
    double sv = round(v, 2);
    NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
    return nf.format(sv);
}

From source file:Main.java

public static String doubleToString(double d, int df) {
    String res = "";
    NumberFormat form;/*from   w w  w . ja va  2s.  c  o  m*/
    form = NumberFormat.getInstance(Locale.US);
    form.setMaximumFractionDigits(df);
    form.setGroupingUsed(false);
    try {
        res = form.format(d);
    } catch (IllegalArgumentException e) {
        res = "";
    }
    return res;
}

From source file:Main.java

/**
 * Convert a string number into a double value
 *
 * @param text the text to be converted to number
 * @return the double value/*from   www. j a  va 2s . c o  m*/
 */
private static double stringToDouble(String text) {

    text = text.replaceAll(",", ".");
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    try {
        return nf.parse(text).doubleValue();
    } catch (ParseException e) {
        Log.e(TAG, e.getMessage(), e);
        return 0.0;
    }
}

From source file:Main.java

/**
 * Formats decimal number for specified locale
 *
 * @param v      Value to format/*w  ww .ja va  2  s. com*/
 * @param locale Locale
 * @return Formatted string
 */
public static String formatBigDecimal(BigDecimal v, Locale locale) {
    NumberFormat numberFormat = NumberFormat.getInstance(locale);

    int scale = 2;
    if (v.intValue() >= 100) {
        scale = 1;
    }

    return numberFormat.format(v.setScale(scale, RoundingMode.UP));
}

From source file:Main.java

/**
 * Convert time to a string//from w ww  .ja  va2 s.  com
 *
 * @param millis e.g.time/length from file
 * @return Formatted string (hh:)mm:ss
 */
public static String millisToString(long millis) {
    boolean negative = millis < 0;
    millis = Math.abs(millis);

    millis /= 1000;
    int sec = (int) (millis % 60);
    millis /= 60;
    int min = (int) (millis % 60);
    millis /= 60;
    int hours = (int) millis;

    String time;
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    format.applyPattern("00");
    if (millis > 0) {
        time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
    } else {
        time = (negative ? "-" : "") + min + ":" + format.format(sec);
    }

    return time;
}

From source file:com.celements.web.service.CelementsWebScriptService.java

public String getHumanReadableSize(long bytes, boolean si, Locale locale) {
    int unit = si ? 1000 : 1024;
    if (bytes < unit) {
        return bytes + " B";
    }/*from w  ww  . j  a  v  a 2  s  . c o  m*/
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
    NumberFormat decimalFormat = DecimalFormat.getInstance(locale);
    decimalFormat.setMaximumFractionDigits(1);
    decimalFormat.setMinimumFractionDigits(1);
    return String.format("%s %sB", decimalFormat.format(bytes / Math.pow(unit, exp)), pre);
}

From source file:Main.java

private static String millisToString(long millis, boolean text) {
    boolean negative = millis < 0;
    millis = java.lang.Math.abs(millis);

    millis /= 1000;/*from   w w w  . java2s. c o m*/
    int sec = (int) (millis % 60);
    millis /= 60;
    int min = (int) (millis % 60);
    millis /= 60;
    int hours = (int) millis;

    String time;
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    format.applyPattern("00");
    if (text) {
        if (millis > 0)
            time = (negative ? "-" : "") + hours + "h" + format.format(min) + "min";
        else if (min > 0)
            time = (negative ? "-" : "") + min + "min";
        else
            time = (negative ? "-" : "") + sec + "s";
    } else {
        if (millis > 0)
            time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
        else
            time = (negative ? "-" : "") + min + ":" + format.format(sec);
    }
    return time;
}

From source file:de.jlo.talendcomp.json.TypeUtil.java

public static DecimalFormat getNumberFormat(String localeStr) {
    DecimalFormat nf = numberformatMap.get(localeStr);
    if (nf == null) {
        Locale locale = new Locale(localeStr);
        nf = (DecimalFormat) NumberFormat.getInstance(locale);
        numberformatMap.put(localeStr, nf);
    }//from  w w w .j av  a2s. com
    return nf;
}

From source file:com.skubit.iab.activities.TransactionDetailsActivity.java

private static final String formatCurrencyAmount(BigDecimal balanceNumber) {
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
    numberFormat.setMaximumFractionDigits(8);
    numberFormat.setMinimumFractionDigits(4);

    if (balanceNumber.compareTo(BigDecimal.ZERO) == -1) {
        balanceNumber = balanceNumber.multiply(new BigDecimal(-1));
    }//from ww w.  j  a  v a  2s . c  om

    return numberFormat.format(balanceNumber);
}

From source file:com.trenako.format.CurrencyFormatter.java

@Override
protected NumberFormat getNumberFormat(Locale locale) {
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(locale);
    format.setParseBigDecimal(true);// w  w  w  .j a v  a2s .co m
    return format;
}