Here you can find the source of formatRateForDisplay(BigDecimal rate)
public static final String formatRateForDisplay(BigDecimal rate)
//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; } }