วันอาทิตย์ที่ 31 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal pow(int n, MathContext mc)

Description Of BigDecimal: pow(int n, MathContext mc)

The java.math.BigDecimal.pow(int n, MathContext mc) returns a BigDecimal whose value is (thisn ) . The current implementation uses the core algorithm defined in ANSI standard X3.274-1996 with rounding according to the context settings. In general, the returned numerical value is within two ulps of the exact numerical value for the chosen precision. Note that future releases may use a different algorithm with a decreased allowable error bound and increased allowable exponent range.

The X3.274-1996 algorithm is:

-An ArithmeticException exception is thrown if
--abs(n) > 999999999
--mc.precision == 0 and n < 0
--mc.precision > 0 and n has more than mc.precision decimal digits
-if n is zero, ONE is returned even if this is zero, otherwise
--if n is positive, the result is calculated via the repeated squaring technique into a single accumulator. The individual multiplications with the accumulator use the same math context settings as in mc except for a precision increased to mc.precision + elength + 1 where elength is the number of decimal digits in n .
--if n is negative, the result is calculated as if n were positive; this value is then divided into one using the working precision specified above.
--The final value from either the positive or negative case is then rounded to the destination precision.

BigDecimal.pow(int n, MathContext mc) method returns this n using the ANSI standard X3.274-1996 algorithm

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("2.242");

  MathContext mc = new MathContext(4); // 4 precision

  BigDecimal bg2 = bg1.pow(2, mc);

  System.out.println(bg2);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal precision()

Description Of BigDecimal: precision()

The java.math.BigDecimal.precision() returns the precision of this BigDecimal . (The precision is the number of digits in the unscaled value.)

BigDecimal.precision() method returns the precision of this BigDecimal .

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  // assign the result of precision of bg1, bg2 to i1 and i2
  int i1 = bg1.precision();
  int i2 = bg2.precision();

  String str1 = "The precision of " + bg1 + " is " + i1;
  String str2 = "The precision of " + bg2 + " is " + i2;

  // print the values of i1, i2
  System.out.println(str1);
  System.out.println(str2);
 }
}


yengo หรือ buzzcity

วันศุกร์ที่ 29 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal remainder(BigDecimal divisor)

Description Of BigDecimal: remainder(BigDecimal divisor)

The java.math.BigDecimal.remainder(BigDecimal divisor) returns a BigDecimal whose value is (this % divisor).

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

BigDecimal.remainder(BigDecimal divisor) method returns this % divisor.

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  BigDecimal bg3 = bg1.remainder(bg2);

  String str = "The remainder is " + bg3;

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


yengo หรือ buzzcity

วันพฤหัสบดีที่ 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

วันพุธที่ 27 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal round(MathContext mc)

Description Of BigDecimal: round(MathContext mc)

The java.math.BigDecimal.round(MathContext mc) returns a BigDecimal rounded according to the MathContext settings. If the precision setting is 0 then no rounding takes place.

The effect of this method is identical to that of the plus(MathContext) method.

BigDecimal.round(MathContext mc) method returns a BigDecimal rounded according to the MathContext settings.

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");

  MathContext mc = new MathContext(2);
  
  BigDecimal bg3 = bg1.round(mc);

  System.out.println(bg3);
 }
}


yengo หรือ buzzcity

วันอังคารที่ 26 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal scale()

Description Of BigDecimal: scale()

The java.math.BigDecimal.scale() returns the scale of this BigDecimal . If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of -3 means the unscaled value is multiplied by 1000.

BigDecimal.scale() method returns the scale of this BigDecimal .

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  // assign the result of scale on bg1, bg2 to i1,i2
  int i1 = bg1.scale();
  int i2 = bg2.scale();

  String str1 = "The scale of " + bg1 + " is " + i1;
  String str2 = "The scale of " + bg2 + " is " + i2;

  // print the values of i1,i2;
  System.out.println(str1);
  System.out.println(str2);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal scaleByPowerOfTen(int n)

Description Of BigDecimal: scaleByPowerOfTen(int n)

The java.math.BigDecimal.scaleByPowerOfTen(int n) returns a BigDecimal whose numerical value is equal to ( this * 10n ). The scale of the result is (this.scale() - n).

BigDecimal.scaleByPowerOfTen(int n) method returns a BigDecimal whose numerical value is equal to ( this * 10n ). The scale of the result is (this.scale() - n).

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  BigDecimal bg3 = bg1.scaleByPowerOfTen(3);
  BigDecimal bg4 = bg2.scaleByPowerOfTen(-3);

  String str1 = bg1 + " raised to 10 power 3 is " + bg3;
  String str2 = bg2 + " raised to 10 power -3 is " + bg4;

  System.out.println(str1);
  System.out.println(str2);
 }
}


yengo หรือ buzzcity