BigDecimal Add, subtract, multiply, in addition to the detailed __java

Source: Internet
Author: User
Tags numeric value

1. For numbers that do not require any accurate calculation precision, float or double can be used directly, but the BigDecimal class must be used if the result of the exact calculation is required
2, the operation speed than the General + 、-、 *,/to Fast
3, the Basic method description


Add (BigDecimal) BigDecimal the value in the object, and then return the object.
Subtract (BigDecimal) BigDecimal the value in the object, and then returns the object.
Multiply (BigDecimal) multiplies the values in the BigDecimal object, and then returns the object.
Divide (BigDecimal) BigDecimal the value in the object, and then returns the object.
ToString () Converts the numeric value of the BigDecimal object to a string.
Doublevalue () returns the value in the BigDecimal object as a double-precision number.
Floatvalue () returns the value in the BigDecimal object as a single-precision number.
Longvalue () returns the value in the BigDecimal object as a long integer.
Intvalue () returns the value in the BigDecimal object as an integer.
Money.divide (New BigDecimal, 2, bigdecimal.round_half_down); except 100, 2 decimal places, rounded 4, Bigdecimal.setscale () method is used to format the decimal point


Setscale (1) means to keep a decimal number by default by rounding
Setscale (1,bigdecimal.round_down) directly remove the extra decimal places, such as 2.35 will become 2.3
Setscale (1,BIGDECIMAL.ROUND_UP) carry handle, 2.35 becomes 2.4
Setscale (1,bigdecimal.round_half_up) rounded, 2.35 turned 2.4
Setscaler (1,bigdecimal.round_half_down) rounded, 2.35 becomes 2.3, if 5 is down-shed


Note:

Scale refers to the number of digits after your decimal point. For example 123.456 score is 3.
Score () is the method in the BigDecimal class.
For example: BigDecimal B = new BigDecimal ("123.456");
B.scale (), the return is 3.

Roundingmode is a retention pattern for decimals. They are all constant fields in BigDecimal, and there are many kinds.
For example: BIGDECIMAL.ROUND_HALF_UP says is 4 homes 5.

Pubilc BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
I use a BigDecimal object divided by the result of divisor and require that the result be reserved for scale decimal places,
Roundingmode means what the retention pattern is, rounding or otherwise, you can choose for yourself.
4: For the general Add, subtract, multiply method of decimal format is as follows:
BigDecimal mdata = new BigDecimal ("9.655"). Setscale (2, bigdecimal.round_half_up);

5, compared with 0
Standard practice

BigDecimal big_decimal = new BigDecimal ("3");
int R=big_decimal.compareto (Bigdecimal.zero); and 0,zero comparison
if (r==0)//equals
if (r==1)//greater Than
if (r==-1)//less than
Or
if (Big_decimal.equals (Bigdecimal.zero))//is equal to 0

[Java] view plain copy BigDecimal ad = new BigDecimal ("20");
BigDecimal su = new BigDecimal ("10");
BigDecimal mu = new BigDecimal ("5");
BigDecimal di = new BigDecimal ("30");

BigDecimal as = Ad.add (su);/+
BigDecimal sa = ad.subtract (su). Subtract (MU);//Minus
BigDecimal MD = mu.multiply (DI);//Multiply
BigDecimal dm = di.divide (MU)//except

System.out.println ("Add as:" +as+ "minus SA:" +sa+ "by MD:" +md+ "except DM:" +DM);

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.