Java BigDecimal Format formatRateForDisplay(BigDecimal rate)

Here you can find the source of formatRateForDisplay(BigDecimal rate)

Description

format Rate For Display

License

Open Source License

Declaration

public static final String formatRateForDisplay(BigDecimal rate) 

Method Source Code

//package com.java2s;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static final String formatRateForDisplay(BigDecimal rate) {

        String outputString = "0.0000";

        if (null != rate) {

            if (BigDecimal.ZERO.setScale(10, RoundingMode.HALF_UP)
                    .compareTo(rate.setScale(10, RoundingMode.HALF_UP)) == 0) {
                //the price is zero, show '0.0000'
            } else {
                BigDecimal tempRate = rate
                        .setScale(4, RoundingMode.HALF_UP);//correct the rate

                //check if you lose precision
                if (BigDecimal.ZERO.setScale(4, RoundingMode.HALF_UP)
                        .compareTo(tempRate) == 0) {
                    outputString = rate.toPlainString(); //show original
                } else {
                    outputString = tempRate.toPlainString(); //show formatted, standard 4 decimal places
                }//from   w ww  .  j  ava 2 s  .co m
            }
        }
        return outputString;
    }
}

Related

  1. formatPercentage(BigDecimal percentage)
  2. formatPercentDecimalDouble(Object input)
  3. formatQuantity(BigDecimal quantity)
  4. formatQuantity(BigDecimal quantity)
  5. formatRate(BigDecimal rate)
  6. formatRight(BigDecimal pNombre, NumberFormat pNf, int pMaxNumberOfDigit)
  7. formatServiceSpecificDate(Date date)
  8. formatSize(Integer size)
  9. formatStore(long store)