วันอาทิตย์ที่ 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

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

Java java.math BigDecimal shortValueExact()

Description Of BigDecimal: shortValueExact()

The java.math.BigDecimal.shortValueExact() converts this BigDecimal to a short , checking for lost information. If this BigDecimal has a nonzero fractional part or is out of the possible range for a short result then an ArithmeticException is thrown.

BigDecimal.shortValueExact() method returns this BigDecimal converted to a short.

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  short s1 = bg1.shortValueExact();
  short s2 = bg2.shortValueExact();

  String str1 = "Exact short value of " + bg1 + " is " + s1;
  String str2 = "Exact short value of " + bg2 + " is " + s2;

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


yengo หรือ buzzcity

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

Java java.math BigDecimal signum()

Description Of BigDecimal: signum()

The java.math.BigDecimal.signum() returns the signum function of this BigDecimal .

BigDecimal.signum() method returns -1, 0, or 1 as the value of this BigDecimal is negative, zero, or positive.

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  int i1 = bg1.signum();
  int i2 = bg2.signum();
  int i3 = bg3.signum();

  String str1 = "The Result of Signum function on " + bg1 + " is " + i1;
  String str2 = "The Result of Signum function on " + bg2 + " is " + i2;
  String str3 = "The Result of Signum function on " + bg3 + " is " + i3;

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


yengo หรือ buzzcity

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

Java java.math BigDecimal stripTrailingZeros()

Description Of BigDecimal: stripTrailingZeros()

The java.math.BigDecimal.stripTrailingZeros() returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the BigDecimal value 600.0 , which has [ BigInteger , scale ] components equals to [6000, 1], yields 6E2 with [ BigInteger , scale ] components equals to [6, -2]

BigDecimal.stripTrailingZeros() method returns a numerically equal BigDecimal with any trailing zeros removed.

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

  BigDecimal bg3 = bg1.stripTrailingZeros();
  BigDecimal bg4 = bg2.stripTrailingZeros();

  String str1 = bg1 + " after removing trailing zeros " + bg3;
  String str2 = bg2 + " after removing trailing zeros " + bg4;

  // print bg3, bg4 values
  System.out.println(str1);
  System.out.println(str2);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal subtract(BigDecimal subtrahend)

Description Of BigDecimal: subtract(BigDecimal subtrahend)

The java.math.BigDecimal.subtract(BigDecimal subtrahend) returns a BigDecimal whose value is (this - subtrahend) , and whose scale is max(this.scale(), subtrahend.scale()).

BigDecimal.subtract(BigDecimal subtrahend) method returns this - subtrahend

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  BigDecimal bg3 = bg1.subtract(bg2);

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


yengo หรือ buzzcity

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

Java java.math BigDecimal subtract(BigDecimal subtrahend, MathContext mc)

Description Of BigDecimal: subtract(BigDecimal subtrahend, MathContext mc)

The java.math.BigDecimal.subtract(BigDecimal subtrahend, MathContext mc) returns a BigDecimal whose value is (this - subtrahend) , with rounding according to the context settings. If subtrahend is zero then this, rounded if necessary, is used as the result. If this is zero then the result is subtrahend.negate(mc) .

BigDecimal.subtract(BigDecimal subtrahend, MathContext mc) method returns this - subtrahend , 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.123");
  BigDecimal bg2 = new BigDecimal("12.12");

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

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


yengo หรือ buzzcity

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

Java java.math BigDecimal toBigInteger()

Description Of BigDecimal: toBigInteger()

The java.math.BigDecimal.toBigInteger() converts this BigDecimal to a BigInteger.

To have an exception thrown if the conversion is inexact (in other words if a nonzero fractional part is discarded), use the toBigIntegerExact() method.

BigDecimal.toBigInteger() method returns this BigDecimal converted to a BigInteger

Code Example Java BigDecimal

import java.math.BigDecimal;
import java.math.BigInteger;

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

  BigInteger bg3 = bg1.toBigInteger();
  BigInteger bg4 = bg2.toBigInteger();

  System.out.println(bg3);
  System.out.println(bg4);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal toBigIntegerExact()

Description Of BigDecimal: toBigIntegerExact()

The java.math.BigDecimal.toBigIntegerExact() converts this BigDecimal to a BigInteger , checking for lost information. An exception is thrown if this BigDecimal has a nonzero fractional part.

BigDecimal.toBigIntegerExact() method returns this BigDecimal converted to a BigInteger.

Code Example Java BigDecimal

import java.math.BigDecimal;
import java.math.BigInteger;

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

  BigInteger bg3 = bg1.toBigIntegerExact();
  BigInteger bg4 = bg2.toBigIntegerExact();

  System.out.println(bg3);
  System.out.println(bg4);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal toEngineeringString()

Description Of BigDecimal: toEngineeringString()

The java.math.BigDecimal.toEngineeringString() returns a string representation of this BigDecimal , using engineering notation if an exponent is needed.

BigDecimal.toEngineeringString() method returns string representation of this BigDecimal , using engineering notation if an exponent is needed.

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  BigDecimal bg = new BigDecimal("1E+5");

  System.out.println("Engineering string value of " + bg + " is "
    + bg.toEngineeringString());
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal toPlainString()

Description Of BigDecimal: toPlainString()

The java.math.BigDecimal.toPlainString() returns a string representation of this BigDecimal without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result.

BigDecimal.toPlainString() method returns a string representation of this BigDecimal without an exponent field.

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  BigDecimal bg = new BigDecimal("1E+5");

  System.out.println("toPlainString string value of " + bg + " is "
    + bg.toPlainString());
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal toString()

Description Of BigDecimal: toString()

The java.math.BigDecimal.toString() returns the string representation of this BigDecimal , using scientific notation if an exponent is needed.

BigDecimal.toString() method returns string representation of this BigDecimal.

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  BigDecimal bg = new BigDecimal("1E+5");

  System.out.println("String value of " + bg + " is " + bg.toString());
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal ulp()

Description Of BigDecimal: ulp()

The java.math.BigDecimal.ulp() returns the size of an ulp, a unit in the last place, of this BigDecimal . An ulp of a nonzero BigDecimal value is the positive distance between this value and the BigDecimal value next larger in magnitude with the same number of digits. An ulp of a zero value is numerically equal to 1 with the scale of this . The result is stored with the same scale as this so the result for zero and nonzero values is equal to [1, this.scale()] .

BigDecimal.ulp() method returns the size of an ulp of this

Code Example Java BigDecimal

import java.math.BigDecimal;

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

  BigDecimal bg3 = bg1.ulp();
  BigDecimal bg4 = bg2.ulp();

  System.out.println("ULP value of " + bg1 + " is " + bg3);
  System.out.println("ULP value of " + bg2 + " is " + bg4);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal unscaledValue()

Description Of BigDecimal: unscaledValue()

The java.math.BigDecimal.unscaledValue() returns a BigInteger whose value is the unscaled value of this BigDecimal. (Computes (this * 10this.scale()))

BigDecimal.unscaledValue() method returns the unscaled value of this BigDecimal.

Code Example Java BigDecimal

import java.math.BigDecimal;
import java.math.BigInteger;

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

  // assign unscaledValue of bg1,bg2 to bi1,bi2
  BigInteger bi1 = bg1.unscaledValue();
  BigInteger bi2 = bg2.unscaledValue();

  String str1 = "The Unscaled Value of " + bg1 + " is " + bi1;
  String str2 = "The Unscaled Value of " + bg2 + " is " + bi2;

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


yengo หรือ buzzcity

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

Java java.math BigDecimal valueOf(double val)

Description Of BigDecimal: valueOf(double val)

The java.math.BigDecimal.valueOf(double val) translates a double into a BigDecimal , using the double 's canonical string representation provided by the Double.toString(double) method.

Note: This is generally the preferred way to convert a double (or float ) into a BigDecimal , as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double) .

BigDecimal.valueOf(double val) method returns a BigDecimal whose value is equal to or approximately equal to the value of val.

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  Double d = new Double("123.45678");

  BigDecimal bg = BigDecimal.valueOf(d);

  System.out.println(bg);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal valueOf(long val)

Description Of BigDecimal: valueOf(long val)

The java.math.BigDecimal.valueOf(long val) translates a long value into a BigDecimal with a scale of zero. This "static factory method" is provided in preference to a ( long ) constructor because it allows for reuse of frequently used BigDecimal values.

BigDecimal.valueOf(long val) method returns a BigDecimal whose value is val .

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  Long d = new Long("123");

  BigDecimal bg = BigDecimal.valueOf(d);

  System.out.println(bg);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal valueOf(long unscaledVal, int scale)

Description Of BigDecimal: valueOf(long unscaledVal, int scale)

The java.math.BigDecimal.valueOf(long unscaledVal, int scale) translates a long unscaled value and an int scale into a BigDecimal . This "static factory method" is provided in preference to a ( long , int ) constructor because it allows for reuse of frequently used BigDecimal values.

BigDecimal.valueOf(long unscaledVal, int scale) method returns a BigDecimal whose value is (unscaledVal ? 10-scale).

Code Example Java BigDecimal

import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  Long d = new Long("123");

  BigDecimal bg = BigDecimal.valueOf(d, 4);

  System.out.println(bg);
 }
}


yengo หรือ buzzcity

วันเสาร์ที่ 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

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

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

Description Of setScale(int newScale, int roundingMode)

The java.math.BigDecimal.setScale(int newScale, int 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.

The new setScale(int, RoundingMode) method should be used in preference to this legacy method.

BigDecimal.setScale(int newScale, int 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.UP);

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

  System.out.println(str);
 }
}


yengo หรือ buzzcity

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

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

Java java.math MathContext equals(Object x)

Description Of MathContext: equals(Object x)

The java.math.MathContext.equals(Object x) compares this MathContext with the specified Object for equality.

MathContext.equals(Object x) method returns true if and only if the specified Object is a MathContext object which has exactly the same settings as this object.

Code Example Java MathContext

import java.math.MathContext;
import java.math.RoundingMode;

public class BigDecimalExam {
 public static void main(String[] args) {
  MathContext mc1 = new MathContext(2);
  MathContext mc2 = new MathContext(2, RoundingMode.HALF_UP);
  MathContext mc3 = new MathContext(2, RoundingMode.HALF_DOWN);

  Boolean b1 = mc1.equals(mc2);
  Boolean b2 = mc1.equals(mc3);

  // print b1, b2 values
  System.out.println(b1);
  System.out.println(b2);
 }
}


yengo หรือ buzzcity

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

Java java.math MathContext getPrecision()

Description Of MathContext: getPrecision()

The java.math.MathContext.getPrecision() returns the precision setting. This value is always non-negative.

MathContext.getPrecision() method returns an int which is the value of the precision setting.

Code Example Java MathContext

import java.math.MathContext;
import java.math.RoundingMode;

public class BigDecimalExam {
 public static void main(String[] args) {
  MathContext mc1 = new MathContext(2);
  MathContext mc2 = new MathContext(2, RoundingMode.HALF_UP);

  int i1 = mc1.getPrecision();
  int i2 = mc2.getPrecision();

  // print i1, i2 values
  System.out.println(i1);
  System.out.println(i2);
 }
}


yengo หรือ buzzcity

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

Java java.math MathContext getRoundingMode()

Description Of MathContext: getRoundingMode()

The java.math.MathContext.getRoundingMode() returns the roundingMode setting.

This will be one of RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR, RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP, RoundingMode.UNNECESSARY, or RoundingMode.UP.

MathContext.getRoundingMode() method returns a RoundingMode object which is the value of the roundingMode setting.

Code Example Java MathContext

import java.math.MathContext;
import java.math.RoundingMode;

public class BigDecimalExam {
 public static void main(String[] args) {
  MathContext mc1 = new MathContext(2, RoundingMode.DOWN);
  MathContext mc2 = new MathContext(2, RoundingMode.HALF_UP);

  RoundingMode i1 = mc1.getRoundingMode();
  RoundingMode i2 = mc2.getRoundingMode();

  // print i1, i2 values
  System.out.println(i1);
  System.out.println(i2);
 }
}


yengo หรือ buzzcity

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

Java java.math MathContext hashCode()

Description Of MathContext: hashCode()

The java.math.MathContext.hashCode() returns the hash code for this MathContext.

MathContext.hashCode() method returns the hash code for this MathContext.

Code Example Java MathContext

import java.math.MathContext;
import java.math.RoundingMode;

public class BigDecimalExam {
 public static void main(String[] args) {
  MathContext mc1 = new MathContext(2, RoundingMode.DOWN);
  MathContext mc2 = new MathContext(2, RoundingMode.HALF_UP);

  int i1 = mc1.hashCode();
  int i2 = mc2.hashCode();

  // print i1, i2 values
  System.out.println(i1);
  System.out.println(i2);
 }
}


yengo หรือ buzzcity

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

Java java.math MathContext toString()

Description Of MathContext: toString()

The java.math.MathContext.toString() returns the string representation of this MathContext.

The String returned represents the settings of the MathContext object as two space-delimited words (separated by a single space character, '\u0020', and with no leading or trailing white space), as follows:

1. The string "precision=", immediately followed by the value of the precision setting as a numeric string as if generated by the Integer.toString method.

2. The string "roundingMode=", immediately followed by the value of the roundingMode setting as a word. This word will be the same as the name of the corresponding public constant in the RoundingMode enum.

Additional words may be appended to the result of toString in the future if more properties are added to this class.

MathContext.toString() method returns a String representing the context settings.

Code Example Java MathContext

import java.math.MathContext;
import java.math.RoundingMode;

public class BigDecimalExam {
 public static void main(String[] args) {
  MathContext mc1 = new MathContext(2, RoundingMode.DOWN);
  MathContext mc2 = new MathContext(2, RoundingMode.HALF_UP);

  String i1 = mc1.toString();
  String i2 = mc2.toString();

  // print i1, i2 values
  System.out.println(i1);
  System.out.println(i2);
 }
}


yengo หรือ buzzcity

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

Java java.math BigDecimal abs()

Java java.math BigDecimal abs()

Description Of BigDecimal: abs()


The java.math.BigDecimal.abs() returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

BigDecimal.abs() This method returns the absolute value of the called value i.e abs(this).

Code Example Java BigDecimal


import java.math.BigDecimal;

public class BigDecimalExam {
 public static void main(String[] args) {
  BigDecimal de1 = new BigDecimal("456");
  BigDecimal de2 = new BigDecimal("-456");
  System.out.println("Absolute value of " + de1 + " is " + de1.abs()); // Absolute value of 456 is 456
  System.out.println("Absolute value of " + de2 + " is " + de2.abs()); // Absolute value of -456 is 456
 }
}

yengo หรือ buzzcity