Java String Shorten shortenCount(final long count)

Here you can find the source of shortenCount(final long count)

Description

Changes a large number into a binary postfixed version.

License

Open Source License

Parameter

Parameter Description
count a parameter

Declaration

public static String shortenCount(final long count) 

Method Source Code

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

public class Main {
    /**//ww  w. ja v a 2s  .co  m
     * Changes a large number into a binary postfixed version.
     * For example: 10,500 -> 10.5K
     * 
     * @param count
     * @return
     */
    public static String shortenCount(final long count) {
        int unit = 1000;

        // Are there at least 1000?
        if (count < unit) {
            return Long.toString(count);
        }

        // Calculate the exponential
        int exponential = (int) (Math.log(count) / Math.log(unit));

        // Get the posfix char
        char postfix = "KMBT".charAt(exponential - 1);

        return String.format("%.1f%c", (count / Math.pow(unit, exponential)), postfix);
    }
}

Related

  1. shortenClassName(Class clazz, int maxLength)
  2. shortenClassName(String cname)
  3. shortenClassName(String str)
  4. shortenComponentLoggerName(String loggerName)
  5. shortenContent(String s, int maxLength)
  6. shortenCount(int count)
  7. shortenDbName(String dbName, int desiredLength)
  8. shortenDerivedIdentifier(final String derivdedIdentifier)
  9. shortenFileName(String name)