How to display a number with always 2 decimal points using BigDecimal?

后端 未结 6 1364
北荒
北荒 2020-12-06 04:00

I am using BigDecimal to get some price values. Requirement is something like this, what ever the value we fetch from database, the displayed valued should have 2 d

6条回答
  •  渐次进展
    2020-12-06 04:50

    The below code may help.

    protected String getLocalizedBigDecimalValue(BigDecimal input, Locale locale) {
        final NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
        numberFormat.setGroupingUsed(true);
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);
        return numberFormat.format(input);
    }
    

提交回复
热议问题