วันเสาร์ที่ 9 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal setScale(int newScale)

Description Of BigDecimal: setScale(int newScale)

The java.math.BigDecimal.setScale(int newScale) returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal 's. Throws an ArithmeticException if this is not possible.

This call is typically used to increase the scale, in which case it is guaranteed that there exists a BigDecimal of the specified scale and the correct value. The call can also be used to reduce the scale if the caller knows that the BigDecimal has sufficiently many zeros at the end of its fractional part (i.e., factors of ten in its integer value) to allow for the rescaling without changing its value.

This method returns the same result as the two-argument versions of setScale , but saves the caller the trouble of specifying a rounding mode in cases where it is irrelevant.

Note that since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named set X mutate field X . Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated

BigDecimal.setScale(int newScale) method returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  BigDecimal bg1 = new BigDecimal("123.4567");

  BigDecimal bg2 = bg1.setScale(4);

  String str = "The value of " + bg1
    + " after changing the scale to 6 is " + bg2;

  System.out.println(str);
 }
}


yengo หรือ buzzcity

ไม่มีความคิดเห็น :

แสดงความคิดเห็น