Java Number Format formatNumber(double value)

Here you can find the source of formatNumber(double value)

Description

format Number

License

Open Source License

Declaration

public static String formatNumber(double value) 

Method Source Code

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

public class Main {
    public static String formatNumber(double value) {
        if (value < 1000D)
            return String.valueOf(value);
        else if (value < 1000000D)
            return String.valueOf(Math.round(value) / 1000D) + "K";
        else if (value < 1000000000D)
            return String.valueOf(Math.round(value / 1000D) / 1000D) + "M";
        else if (value < 1000000000000D)
            return String.valueOf(Math.round(value / 1000000D) / 1000D) + "B";
        else/*  w  ww  . ja v a 2s  .  co m*/
            return String.valueOf(Math.round(value / 1000000000D) / 1000D) + "T";
    }

    public static String formatNumber(long value) {
        if (value < 1000L)
            return String.valueOf(value);
        else if (value < 1000000L)
            return String.valueOf(Math.round(value) / 1000D) + "K";
        else if (value < 1000000000L)
            return String.valueOf(Math.round(value / 1000L) / 1000D) + "M";
        else if (value < 1000000000000L)
            return String.valueOf(Math.round(value / 1000000L) / 1000D) + "B";
        else if (value < 1000000000000000L)
            return String.valueOf(Math.round(value / 1000000000L) / 1000D) + "T";
        else if (value < 1000000000000000000L)
            return String.valueOf(Math.round(value / 1000000000000L) / 1000D) + "Quad";
        else if (value <= Long.MAX_VALUE)
            return String.valueOf(Math.round(value / 1000000000000000L) / 1000D) + "Quin";
        else
            return "Something is very broken!!!!";
    }

    public static double round(double number, double multiplier) {
        return Math.round(number * multiplier) / multiplier;
    }
}

Related

  1. formatNumber(double dd)
  2. formatNumber(double dscore)
  3. formatNumber(double number, int decimalPlaces)
  4. formatNumber(Double val, int numDec, int width)
  5. formatNumber(double value)
  6. formatNumber(final long number)
  7. formatNumber(final String number)
  8. formatNumber(final StringBuilder buf, final long number)
  9. formatNumber(float num, int fragmentSize)