Java BigDecimal.setScale(int newScale, RoundingMode roundingMode)

Syntax

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

public BigDecimal setScale(int newScale,   RoundingMode roundingMode)

Example

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


/*from w  w  w .ja  va2 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.12678");

    // set scale of bg1 to 2 in bg2 using floor as rounding mode
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.FLOOR);

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

    // print bg2 value
    System.out.println(str);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.math »




BigDecimal
BigInteger