Java BigInteger Calculate numberToShortString(BigInteger number)

Here you can find the source of numberToShortString(BigInteger number)

Description

Returns a string representing the shortened numeric value.

License

Apache License

Parameter

Parameter Description
number The number to be displayed

Return

Display string

Declaration

public static String numberToShortString(BigInteger number) 

Method Source Code

//package com.java2s;
/**//from   w ww.j  a  va 2s .  c o  m
 * Copyright 2011 Google Inc.
 * Copyright 2013-2014 Ronald W Hoffman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigInteger;
import java.math.BigDecimal;

public class Main {
    /** Constant 1,000 */
    private static final BigInteger DISPLAY_1K = new BigInteger("1000");
    /** Constant 1,000,000 */
    private static final BigInteger DISPLAY_1M = new BigInteger("1000000");
    /** Constant 1,000,000,000 */
    private static final BigInteger DISPLAY_1G = new BigInteger("1000000000");
    /** Constant 1,000,000,000,000 */
    private static final BigInteger DISPLAY_1T = new BigInteger("1000000000000");
    /** Constant 1,000,000,000,000,000 */
    private static final BigInteger DISPLAY_1P = new BigInteger("1000000000000000");

    /**
     * Returns a string representing the shortened numeric value.  For example,
     * the value 1,500,000 will be returned as 1.500M.
     *
     * @param       number          The number to be displayed
     * @return      Display string
     */
    public static String numberToShortString(BigInteger number) {
        int scale;
        String suffix;
        BigDecimal work;
        if (number.compareTo(DISPLAY_1P) >= 0) {
            scale = 15;
            suffix = "P";
        } else if (number.compareTo(DISPLAY_1T) >= 0) {
            scale = 12;
            suffix = "T";
        } else if (number.compareTo(DISPLAY_1G) >= 0) {
            scale = 9;
            suffix = "G";
        } else if (number.compareTo(DISPLAY_1M) >= 0) {
            scale = 6;
            suffix = "M";
        } else if (number.compareTo(DISPLAY_1K) >= 0) {
            scale = 3;
            suffix = "K";
        } else {
            scale = 0;
            suffix = "";
        }
        if (scale != 0)
            work = new BigDecimal(number, scale);
        else
            work = new BigDecimal(number);

        return String.format("%3.3f%s", work.floatValue(), suffix);
    }
}

Related

  1. multi_exponent(BigInteger[] base, BigInteger[] exponent, BigInteger modulus)
  2. multiply(BigInteger x, BigInteger y)
  3. negative(BigInteger n)
  4. normalizarParaBigInteger(Number numero, RoundingMode modo)
  5. numberToIpv4(BigInteger ipNumber)
  6. order(BigInteger a, BigInteger p, BigInteger f[], BigInteger e[])
  7. parseBigInteger(String s, BigInteger defaultValue)
  8. parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative)
  9. parseScaledNonNegativeBigInteger(String str)