Java processing data-related methods

Source: Internet
Author: User

First, BigDecimal related methods

1.bigdecimal.setscale (int newscale, roundingmode roundingmode) format decimal point

Setscale (1) indicates that a decimal number is reserved and is rounded by default
Setscale (1,bigdecimal.round_down) directly removes the extra decimal digits, such as 2.35 will become 2.3
Setscale (1,BIGDECIMAL.ROUND_UP) Carry processing, 2.35 becomes 2.4
Setscale (1,bigdecimal.round_half_up) rounded, 2.35 turned 2.4

Setscale (1,bigdecimal.round_half_down) rounded, 2.35 becomes 2.3, if 5 is down

notes:(1). Parameter newscale: Refers to the number of decimal places you want to keep (2). The parameter roundingmode is a reserved mode for decimals. They are all constant fields in BigDecimal, and there are many kinds.For example: Bigdecimal.round_half_up means 4 to 5 in.

2.bigdecimal.scale (): Returns the number of digits after the decimal point.
Cases: BigDecimal b = new BigDecimal ("123.456"); B.scale ();--------------return 33.bigdecimal.divide (BigDecimal divisor, int scale, int roundingmode): divided by
(1) The result of dividing the BigDecimal object by divisor;(2) This result retains a scale decimal place, (3) Roundingmode is a retention mode, such as rounding, etc.;

4.add, subtract, multiply: add, subtract, multiply:

BigDecimal B1 = new BigDecimal ("9.655");
BigDecimal b2 = new BigDecimal ("5");

B1.add (B2); --------B1+b2 14.655

B1.subtract (B2); ----B1-B2 4.655

B1.multiply (B2); ----B1*B2 48.275

Java processing data-related methods

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.