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

Java java.math BigDecimal remainder(BigDecimal divisor, MathContext mc)

Description Of BigDecimal: remainder(BigDecimal divisor, MathContext mc)

The java.math.BigDecimal.remainder(BigDecimal divisor, MathContext mc) returns a BigDecimal whose value is (this % divisor) , with rounding according to the context settings. The MathContext settings affect the implicit divide used to compute the remainder. The remainder computation itself is by definition exact. Therefore, the remainder may contain more than mc.getPrecision() digits.

The remainder is given by this.subtract(this.divideToIntegralValue(divisor, mc).multiply(divisor)) . Note that this is not the modulo operation (the result can be negative).

BigDecimal.remainder(BigDecimal divisor, MathContext mc) method returns this % divisor , rounded as necessary.

Code Example Java BigDecimal

import java.math.BigDecimal;
import java.math.MathContext;

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

  MathContext mc = new MathContext(2); // 2 precision
  
  BigDecimal bg3 = bg1.remainder(bg2, mc);

  String str = "The remainder is " + bg3;

  // print the value of bg3
  System.out.println(str);
 }
}


yengo หรือ buzzcity

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

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