Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public final String format(long number) 

Source Link

Document

Specialization of format.

Usage

From source file:Main.java

public static String getNumberWithFractionDigit(double value, int fracLength) {
    NumberFormat nf = NumberFormat.getInstance();

    nf.setMaximumFractionDigits(fracLength);
    nf.setMinimumFractionDigits(fracLength);

    return nf.format(value);
}

From source file:Main.java

public synchronized static String getPriceByFormat(float price) {
    NumberFormat format = NumberFormat.getInstance();
    format.setMinimumFractionDigits(2);/*from w w  w .jav  a  2  s.c  om*/
    format.setMaximumFractionDigits(2);
    String priceNum = format.format(price);
    return priceNum;
}

From source file:Main.java

public static String numFormat(int len, int num) {
    NumberFormat formatter = NumberFormat.getNumberInstance();
    formatter.setMinimumIntegerDigits(len);
    formatter.setGroupingUsed(false);// ww  w . ja va 2s. co  m
    String numStr = formatter.format(num);
    return numStr;
}

From source file:NumberFormatDemo.java

static public void displayPercent(Locale currentLocale) {

    Double percent = new Double(0.75);
    NumberFormat percentFormatter;
    String percentOut;//  w w w  . j a  v a  2 s  .  com

    percentFormatter = NumberFormat.getPercentInstance(currentLocale);
    percentOut = percentFormatter.format(percent);
    System.out.println(percentOut + "   " + currentLocale.toString());
}

From source file:Main.java

public static String getMatchingThresholdToString(int value) {
    double p = -value / 12.0;
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMaximumFractionDigits(Math.max(0, (int) Math.ceil(-p) - 2));
    nf.setMinimumIntegerDigits(1);/*from w ww.  ja va 2  s .  c  o  m*/
    return nf.format(Math.pow(10, p));
}

From source file:Main.java

public static String formatStringTwoDecPercent(float number) {
    NumberFormat formatter = NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(0);
    formatter.setMaximumFractionDigits(2);

    return String.valueOf(formatter.format(number) + "%");
}

From source file:com.example.awesomedogs.util.Stuff.java

/**
 * Converts some currency cents to a more readable format.
 * /*  w  ww . ja  v  a2  s  . co  m*/
 * @param cents Amount in cents, e.g. 123.
 * @return Formatted value, e.g. "1.23".
 */
public static String formatCents(int cents) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    return nf.format(((double) cents / 100));
}

From source file:NumberFormatDemo.java

static public void displayCurrency(Locale currentLocale) {

    Double currency = new Double(9876543.21);
    NumberFormat currencyFormatter;
    String currencyOut;//  ww  w  .j a v  a  2  s . c  om

    currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
    currencyOut = currencyFormatter.format(currency);
    System.out.println(currencyOut + "   " + currentLocale.toString());
}

From source file:Test.java

static void displayLocalizedData(Locale l, long number, Date date) {
    NumberFormat nf = NumberFormat.getInstance(l);
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, l);
    System.out.printf("Locale: %s\nNumber: %s\nDate: %s\n\n", l.getDisplayName(), nf.format(number),
            df.format(date));//w  w w .  j a  v a2s. c o m
}

From source file:Main.java

protected static String format(String value) {
    Double myNumber = Double.valueOf(value);
    NumberFormat format = NumberFormat.getInstance();
    format.setMinimumFractionDigits(3);//www  . ja va2  s.c  o  m
    format.setMaximumFractionDigits(3);
    format.setGroupingUsed(false);
    return format.format(myNumber);
}