BigDecimal in Java Half_even

Source: Internet
Author: User

What does Half_even mean? If the number you want to throw away is not 5, you need to look at the parity of the number in front of this number, if it is odd, use the half_up rule, if it is even use the Half_down rule, if the number you want to discard is 5, round to its even neighbor;

What does it mean to round to its even-numbered neighbors?

Example:

System.out.println ("=================="); for (int i = 0; i < ten;  i + +) {StringBuffer sb = new StringBuffer (); Sb.append (i); Sb.append (". 5"); BigDecimal BDX = new BigDecimal (sb.tostring ()); SYSTEM.OUT.PRINTLN (SB + "" +bdx.setscale (0, Roundingmode.half_even));} System.out.println ("=================="); for (int i = 0; i < ten;  i + +) {StringBuffer sb = new StringBuffer (); Sb.append ("0."); Sb.append (i); Sb.append ("5"); BigDecimal BDX = new BigDecimal (sb.tostring ()); SYSTEM.OUT.PRINTLN (SB + "" +bdx.setscale (1, Roundingmode.half_even));}

The output is:

==================
0.5 0-->0.5 Distance 0 and 1 are 0.5, but 0 is even, all round to 0
1.5 2
2.5 2
3.5 4
4.5 4
5.5 6
6.5 6
7.5 8
8.5 8
9.5 10
==================
0.05 0.0
0.15 0.2
0.25 0.2
0.35 0.4
0.45 0.4
0.55 0.6
0.65 0.6
0.75 0.8
0.85 0.8
0.95 1.0

BigDecimal in Java Half_even

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.