Java Data Type Tutorial - Java BigDecimal.setScale(int newScale, int roundingMode)








Syntax

BigDecimal.setScale(int newScale, int roundingMode) has the following syntax.

public BigDecimal setScale(int newScale,   int roundingMode)

Example

In the following code shows how to use BigDecimal.setScale(int newScale, int roundingMode) method.

//  w ww  .  j  ava2  s  . c o m
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {

  public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.12345678");

    // set scale of bg1 to 2 in bg2
    // 0 specifies ROUND_UP
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.UP);

    String str = bg1 + " after changing the scale to 2 and rounding is " + bg2;

    System.out.println(str);
  }
}

The code above generates the following result.