Use of BigDecimal and currency for calculation of amount __java

Source: Internet
Author: User
Tags mul

Import Java.math.BigDecimal;


public class Bigdecimalutil {


/**

* Plus

*/

public static BigDecimal Add (DOUBLEV1, Double v2) {

BigDecimal B1 = new BigDecimal (v1.tostring ());

BigDecimal b2 = new BigDecimal (v2.tostring ());

Return B1.add (B2);

}


/**

* Minus

*/

public static BigDecimal Sub (DOUBLEV1, Double v2) {

BigDecimal B1 = new BigDecimal (v1.tostring ());

BigDecimal b2 = new BigDecimal (v2.tostring ());

Return B1.subtract (B2);

}


/**

* Multiply

*/

public static BigDecimal Mul (DOUBLEV1, Double v2) {

BigDecimal B1 = new BigDecimal (v1.tostring ());

BigDecimal b2 = new BigDecimal (v2.tostring ());

Return b1.multiply (B2);

}


/**

* except

*/

public static BigDecimal Div (DOUBLEV1, Double v2) {

BigDecimal B1 = new BigDecimal (v1.tostring ());

BigDecimal b2 = new BigDecimal (v2.tostring ());

Rounded, reserving two decimal places

Return B1.divide (B2, 2, Bigdecimal.round_half_down);

}


/**

*

* @param args

*/

Public staticvoid Main (string[] args) {


Double a=0.06;

Double b=0.01;

System.out.println (Add (a,b));

System.out.println (Sub (a,b));

System.out.println (Mul (a,b));

System.out.println (Div (a,b));


BigDecimal aa=new BigDecimal ("10.5");

BigDecimal bb=new BigDecimal ("10.50");

System.out.println (Aa.equals (BB));

System.out.println (134/100);



}


}


Use of 2BigDecimal
Some basic methods of BigDecimal, specific reference http://www.apihome.cn/api/java/BigDecimal.html

BigDecimal ABS ()
Returns BigDecimal with the value of the BigDecimal, whose scale is This.scale ().
BigDecimal ABS (Mathcontext MC)
Returns the BigDecimal for this BigDecimal absolute value (rounded according to the context setting).
BigDecimal Add (BigDecimal augend)
Returns a BigDecimal with a value of (this + augend) with a scale of Max (This.scale (), Augend.scale ()).
BigDecimal Add (BigDecimal augend, Mathcontext MC)
Returns the BigDecimal whose value is (this + augend) (rounded according to the context setting).
Byte Bytevalueexact ()
Convert this BigDecimal to byte to check for missing information.
Int CompareTo (BigDecimal val)
Compares this BigDecimal to the specified BigDecimal.
BigDecimal Divide (BigDecimal divisor)
Returns a BigDecimal with a value of (this/divisor) whose preferred scale is (This.scale ()-Divisor.scale ()), or if the exact quotient value cannot be represented (because it has an infinite decimal extension), Throws the ArithmeticException.
BigDecimal Divide (BigDecimal divisor, int roundingmode)
Returns a BigDecimal with a value of (this/divisor) with a scale of This.scale ().
BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
Returns a BigDecimal with a value of (this/divisor) whose scale is the specified scale.
BigDecimal Divide (BigDecimal divisor, int scale, Roundingmode roundingmode)
Returns a BigDecimal with a value of (this/divisor) whose scale is the specified scale.
BigDecimal Divide (BigDecimal divisor, Mathcontext MC)
Returns the BigDecimal whose value is (this/divisor) (rounded according to the context setting).
BigDecimal Divide (BigDecimal divisor, Roundingmode roundingmode)
Returns a BigDecimal with a value of (this/divisor) with a scale of This.scale ().
Bigdecimal[] Divideandremainder (BigDecimal divisor)
Returns a two-element BigDecimal array that contains the results of the dividetointegralvalue followed by the remainder of the two operand calculations.
Bigdecimal[] Divideandremainder (BigDecimal divisor, Mathcontext MC)
Returns a two-element BigDecimal array that contains the results of the dividetointegralvalue, followed by the remainder result of rounding the two operands based on the context setting.
BigDecimal Dividetointegralvalue (BigDecimal divisor)
Returns BigDecimal, whose value is the integer portion of the quotient value (this/divisor) that is rounded down.
BigDecimal Dividetointegralvalue (BigDecimal divisor, Mathcontext MC)
Returns the BigDecimal, whose value is the integer portion of (this/divisor).
Double Doublevalue ()
Converts this BigDecimal to double.
Boolean Equals (Object x)
Compares the equality of this BigDecimal with the specified Object.
Float Floatvalue ()
Converts this BigDecimal to float.
Int Hashcode ()
Returns the hash code for this BigDecimal.
Int Intvalue ()
Converts this BigDecimal to an int.
Int Intvalueexact ()
Convert this BigDecimal to int to check for missing information.
Long Longvalue ()
Converts this BigDecimal to long.
Long Longvalueexact ()
Converts this BigDecimal to long to check for missing information.
BigDecimal Max (BigDecimal val)
Returns the maximum value for this BigDecimal and Val.
BigDecimal Min (BigDecimal val)
Returns the minimum value for this BigDecimal and Val.
BigDecimal Movepointleft (int n)
Returns a BigDecimal, which is equivalent to moving the decimal point of the value to the left n bit.
BigDecimal Movepointright (int n)
Returns a BigDecimal, which is equivalent to moving the decimal point of the value to the right n bit.
BigDecimal Multiply (BigDecimal multiplicand)
Returns a BigDecimal with a value of (Thisxmultiplicand) with a scale of (This.scale () + Multiplicand.scale ()).
BigDecimal Multiply (BigDecimal multiplicand, Mathcontext MC)
Returns the BigDecimal whose value is (Thisxmultiplicand) (rounded according to the context setting).
BigDecimal Negate ()
Returns BigDecimal with a value of (-this) with a scale of This.scale ().
BigDecimal Negate (Mathcontext MC)
Returns the BigDecimal whose value is (-this) (rounded according to the context setting).
BigDecimal Plus ()
Returns BigDecimal with a value of (+this) with a scale of This.scale ().
BigDecimal Plus (Mathcontext MC)
Returns the BigDecimal whose value is (+this) (rounded according to the context setting).
BigDecimal Pow (int n)
Returns the BigDecimal whose value is (THISN) and accurately calculates the power so that it has infinite precision.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.