Here you can find the source of numberToShortString(BigInteger number)
Parameter | Description |
---|---|
number | The number to be displayed |
public static String numberToShortString(BigInteger number)
//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); } }