Java BigDecimal getFormattedBigDecimal(BigDecimal value)

Here you can find the source of getFormattedBigDecimal(BigDecimal value)

Description

get Formatted Big Decimal

License

Open Source License

Declaration

public static String getFormattedBigDecimal(BigDecimal value) 

Method Source Code

//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)]);
        }
    }
}

Related

  1. getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude)
  2. getDividedBigDecimal(final int nDividend, final int nDivisor)
  3. getDoubleBigDecimal(String amount, int afterDot)
  4. getEndTimeByManDay(Date serviceStartTime, BigDecimal manDay)
  5. getFloatingPoint(Console console, Function validator)
  6. getFraction(BigDecimal b)
  7. getFractionalPart(BigDecimal value)
  8. getId(BigDecimal total, BigDecimal step)
  9. getIntLength(BigDecimal val)