Java Locale Format format(Number number)

Here you can find the source of format(Number number)

Description

Formats the specified number to be readable to the human eye.

License

Open Source License

Parameter

Parameter Description
number The number to format.

Return

The formatted number, represented as a String .

Declaration

public static String format(Number number) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    /**/*from  www.  j  ava  2  s.c o  m*/
     * A format for representing thousands, millions and billions.
     */
    private static final String FORMAT = " KMB";
    /**
     * The default number decimal format.
     */
    private static final NumberFormat DECIMAL_FORMAT = new DecimalFormat("#,###.#");
    /**
     * The default number format.
     */
    private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);

    /**
     * Formats the specified number to be readable to the human eye. The number
     * may not be larger than that of 2<sup>31</sup>.
     *
     * @param number The number to format.
     * @return The formatted number, represented as a {@code String}.
     */
    public static String format(Number number) {
        StringBuilder bldr = new StringBuilder();
        int power = (int) Math.log10(number.doubleValue());
        double value = Math.floor(number.doubleValue() / Math.pow(10, power / 3 * 3));
        bldr.append(DECIMAL_FORMAT.format(value)).append(FORMAT.charAt(power / 3));
        bldr.append(" (").append(NUMBER_FORMAT.format(number)).append(")");
        return bldr.toString();
    }
}

Related

  1. format(final double num, final int prec)
  2. format(final String bundleKey, final String messageKey, final Locale locale, final Object... arguments)
  3. format(int[] a)
  4. format(Locale locale, ResourceBundle bundle, String key, Object... args)
  5. format(Locale locale, String key, Object... arguments)
  6. format(Number number, Locale locale)
  7. format(Object input, int style)
  8. format(String bundleName, Locale locale, String key, Object arg1)
  9. format(String format, Date date, Locale locale)