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 currencyFormat(double value, int fractionDigits) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    nf.setMaximumFractionDigits(fractionDigits);
    return (nf.format(value));
}

From source file:Main.java

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

From source file:Main.java

/**
 * Str format.//from   ww w .j a v a 2  s . c  o  m
 * 
 * @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

private static JPanel createPanel() {
    JPanel panel = new JPanel();
    DefaultTableModel model = new DefaultTableModel() {
        @Override// ww  w .j a  v a2  s .co m
        public Class<?> getColumnClass(int col) {
            if (col == 0) {
                return Icon.class;
            } else {
                return Double.class;
            }
        }
    };
    model.setColumnIdentifiers(new Object[] { "Book", "Cost" });
    for (int i = 0; i < 42; i++) {
        model.addRow(new Object[] { ICON, Double.valueOf(i) });
    }
    JTable table = new JTable(model);
    table.setDefaultRenderer(Double.class, new DefaultTableCellRenderer() {
        @Override
        protected void setValue(Object value) {
            NumberFormat format = NumberFormat.getCurrencyInstance();
            setText((value == null) ? "" : format.format(value));
        }
    });
    table.setRowHeight(ICON.getIconHeight());
    panel.add(new JScrollPane(table) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }
    });
    return panel;
}

From source file:Main.java

/**
 * Converts a byte-count to a formatted string suitable for display to the user.
 * For instance:/* ww w . j a  va  2  s. c  o m*/
 * <ul>
 * <li><code>format(918)</code> returns <em>"918 B"</em>.</li>
 * <li><code>format(98765)</code> returns <em>"96 KB"</em>.</li>
 * <li><code>format(1238476)</code> returns <em>"1.2 MB"</em>.</li>
 * </ul>
 * This method assumes that 1 KB is 1024 bytes.
 * To get a localized string, please use formatLocalizedBytes instead.
 *
 * @param byteCount The number of bytes.
 * @return The formatted string.
 */
public static synchronized String formatBytes(long byteCount) {

    // More than 1 GB?
    if (byteCount >= 1024 * 1024 * 1024) {
        NumberFormat gigaByteFormat = GIGA_BYTE_FORMAT;
        return gigaByteFormat.format((double) byteCount / (1024 * 1024 * 1024));
    }

    // More than 1 MB?
    if (byteCount >= 1024 * 1024) {
        NumberFormat megaByteFormat = MEGA_BYTE_FORMAT;
        return megaByteFormat.format((double) byteCount / (1024 * 1024));
    }

    // More than 1 KB?
    if (byteCount >= 1024) {
        NumberFormat kiloByteFormat = KILO_BYTE_FORMAT;
        return kiloByteFormat.format((double) byteCount / 1024);
    }

    return byteCount + " B";
}

From source file:Main.java

public static String getTimeFromLong(long l) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(l);//from  w ww .j av  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

/**
 * Converts a length of bytes to MB.//  ww  w .j  a  v a2  s  .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 setLocaleNumberFormat(Locale locale, Number number) {

    NumberFormat formatter = NumberFormat.getInstance(locale);
    formatter.setMaximumFractionDigits(4);
    String localeFormattedNumber = formatter.format(number);
    return localeFormattedNumber;

}

From source file:Main.java

public static void stopCounter(String tag) {
    long stopTime = System.currentTimeMillis();
    if (listTimes.containsKey(tag)) {
        long startTime = listTimes.get(tag);
        long elapsedTime = stopTime - startTime;
        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
        System.out.println("#" + tag + "# {End counting :" + numberFormat.format(elapsedTime) + " ms }");
        listTimes.remove(tag);// ww  w.  j  a v  a  2  s.  c  o m
    } else
        Log.e(tag, "NO KEY IN HASHMAP To calculate Time");

}

From source file:com.squarespace.template.plugins.platform.PlatformUtils.java

/**
 * Like money-format (e.g. 0.00) but with a currency (dollar) sign and parentheses on negative values.
 * Fully internationalized. Uses NumberFormat under the hood.
 *//*from w w  w  .java 2s.com*/
public static String formatBookkeeperMoney(double cents, Locale locale) {
    NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
    return formatter.format(cents / 100);
}