Java Currency Format removeTrailingZeroes(BigDecimal value, boolean isCurrency)

Here you can find the source of removeTrailingZeroes(BigDecimal value, boolean isCurrency)

Description

Convert BigDecimal to currency or quantity.

License

Apache License

Parameter

Parameter Description
value a parameter
isCurrency a parameter

Return

value, with trailing zeroes removed

Declaration

public static BigDecimal removeTrailingZeroes(BigDecimal value, boolean isCurrency) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Main {
    /**/*from w w w. ja v  a  2  s .c o  m*/
     * Convert BigDecimal to currency or quantity. Currency has always at least 2 fractions and quantity 0 or more. Maximum precision allowed
     * is 6 decimal places (numbers after decimal point)
     * 
     * @param value
     * @param isCurrency
     * @return value, with trailing zeroes removed
     */
    public static BigDecimal removeTrailingZeroes(BigDecimal value, boolean isCurrency) {
        if (value == null) {
            return null;
        }
        DecimalFormat df;
        if (isCurrency) {
            df = new DecimalFormat("0.00####");
        } else {
            df = new DecimalFormat("0.######");
        }
        String format = df.format(value);
        return new BigDecimal(format.replace(",", "."));
    }
}

Related

  1. formatCurrencyWithCurrencyUnit(double amt, int fractionDigits)
  2. getCurrencyFormat()
  3. getCurrencyFormat(final Locale locale)
  4. getCurrencyFormatter()
  5. parseCurrency(String currency)
  6. roundToCurrencyString(double amount)
  7. toCurrency(Object objectValue)
  8. toCurrencyAmountStr(Long microAmount)
  9. toCurrencyFormat(String input)