วันพฤหัสบดีที่ 7 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal setScale(int newScale, RoundingMode roundingMode)

Description Of BigDecimal: setScale(int newScale, RoundingMode roundingMode)

The java.math.BigDecimal.setScale(int newScale, RoundingMode roundingMode) 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. If the scale is reduced by the operation, the unscaled value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division.

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, RoundingMode roundingMode) 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;
import java.math.RoundingMode;

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

  BigDecimal bg2 = bg1.setScale(4, RoundingMode.FLOOR);

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

  System.out.println(str);
 }
}


yengo หรือ buzzcity

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

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