format BigDecimal by scale and rounding - Java java.math

Java examples for java.math:BigDecimal Format

Description

format BigDecimal by scale and rounding

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal value = new BigDecimal("1234");
        System.out.println(format(value));
    }/*www  .  j  ava 2 s. c  o m*/

    private static final int SCALE = 2;

    public static BigDecimal format(BigDecimal value) {
        if (value == null)
            return null;
        return value.setScale(SCALE, BigDecimal.ROUND_HALF_UP);
    }
}

Related Tutorials