Here you can find the source of getFormattedBigDecimal(BigDecimal value)
public static String getFormattedBigDecimal(BigDecimal value)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; import java.math.MathContext; public class Main { private static final String[] UNIT = new String[] { "", "K", "M", "G", "T", "P", "E", "Z", "Y" }; private static final BigDecimal THOUSAND = new BigDecimal("1000"); private static final BigDecimal DISPLAY_MAX = new BigDecimal(Double.MAX_VALUE).multiply(THOUSAND.pow(9)); public static String getFormattedBigDecimal(BigDecimal value) { if (value.compareTo(DISPLAY_MAX) > 0) { return "very high"; }//w w w .j av a 2 s. co m int unitIndex = 0; for (; unitIndex < UNIT.length && value.compareTo(THOUSAND) > 0; unitIndex++) { value = value.divide(THOUSAND); } if (unitIndex == 0) { return String.format("%d", value.round(MathContext.DECIMAL32).intValue()); } else { return String.format("%.2f%s", value.round(MathContext.DECIMAL64).doubleValue(), UNIT[Math.min(unitIndex, UNIT.length - 1)]); } } }