Java rounding and Java round methods

Source: Internet
Author: User

1 ImportJava.math.BigDecimal;2 ImportJava.text.DecimalFormat;3 4  Public classTestgetint {5      Public Static voidMain (String[]args) {6         Doublei = 2,7j = 2.1,8K = 2.5,9m = 2.9;TenSystem.out.println ("Rounding off Decimal rounding: Math.floor (2) =" + (int) Math.floor (i)); OneSystem.out.println ("Rounding off Decimal rounding: Math.floor (2.1) =" + (int) Math.floor (j)); ASystem.out.println ("Rounding off Decimal rounding: math.floor (2.5) =" + (int) Math.floor (k)); -System.out.println ("Rounding off Decimal rounding: Math.floor (2.9) =" + (int) Math.floor (m));  -  theSystem.out.println ("Rounded rounding: (2) =" +NewBigDecimal ("2"). Setscale (0, bigdecimal.round_half_up)); -System.out.println ("Rounded rounding: (2.1) =" +NewBigDecimal ("2.1"). Setscale (0, bigdecimal.round_half_up)); -System.out.println ("Rounded rounding: (2.5) =" +NewBigDecimal ("2.5"). Setscale (0, bigdecimal.round_half_up)); -System.out.println ("Rounded rounding: (2.9) =" +NewBigDecimal ("2.9"). Setscale (0, bigdecimal.round_half_up)); +  -System.out.println ("Rounding: Math.ceil (2) =" + (int) Math.ceil (i)); +System.out.println ("Rounding: Math.ceil (2.1) =" + (int) Math.ceil (j)); ASystem.out.println ("Rounding: math.ceil (2.5) =" + (int) Math.ceil (k)); atSystem.out.println ("Rounding: Math.ceil (2.9) =" + (int) Math.ceil (m)); -  -System.out.println ("Rounding out Fractional rounding: Math.floor (-2) =" + (int) Math.floor (-i)); -System.out.println ("Rounding out Fractional rounding: Math.floor (-2.1) =" + (int) Math.floor (-j)); -System.out.println ("Rounding out Fractional rounding: Math.floor (-2.5) =" + (int) Math.floor (-k)); -System.out.println ("Rounding out Fractional rounding: Math.floor (-2.9) =" + (int) Math.floor (-m)); inSystem.out.println ("Rounded rounding: (-2) =" +NewBigDecimal ("-2"). Setscale (0, bigdecimal.round_half_up)); -System.out.println ("Rounded rounding: (-2.1) =" +NewBigDecimal (" -2.1"). Setscale (0, bigdecimal.round_half_up)); toSystem.out.println ("Rounded rounding: (-2.5) =" +NewBigDecimal (" -2.5"). Setscale (0, bigdecimal.round_half_up)); +System.out.println ("Rounded rounding: (-2.9) =" +NewBigDecimal (" -2.9"). Setscale (0, bigdecimal.round_half_up)); -  theSystem.out.println ("Rounding: Math.ceil (-2) =" + (int) Math.ceil (-i)); *System.out.println ("Rounding: Math.ceil (-2.1) =" + (int) Math.ceil (-j)); $System.out.println ("Rounding: Math.ceil (-2.5) =" + (int) Math.ceil (-k));Panax NotoginsengSystem.out.println ("Rounding: Math.ceil (-2.9) =" + (int) Math.ceil (-m)); -     } the}

//_____________________________

Rounding off Decimal rounding: Math.floor (2) =2

Rounding off Decimal rounding: Math.floor (2.1) =2

Rounding off Decimal rounding: Math.floor (2.5) =2

Rounding off Decimal rounding: Math.floor (2.9) =2

Rounding Round rounding: (2) =2

Rounding Round rounding: (2.1) =2

Rounding Round rounding: (2.5) =3

Rounding Round rounding: (2.9) =3

Rounding: Math.ceil (2) =2

Rounding: Math.ceil (2.1) =3

Rounding: Math.ceil (2.5) =3

Rounding: Math.ceil (2.9) =3

Rounding off Decimal rounding: Math.floor (-2) =-2

Rounding off Decimal rounding: Math.floor (-2.1) =-3

Rounding off Decimal rounding: Math.floor (-2.5) =-3

Rounding off Decimal rounding: Math.floor (-2.9) =-3

Rounded rounding: (-2) =-2

Rounded rounding: (-2.1) =-2

Rounded rounding: (-2.5) =-3

Rounded rounding: (-2.9) =-3

Rounding: Math.ceil (-2) =-2

Rounding: Math.ceil (-2.1) =-2

Rounding: Math.ceil (-2.5) =-2

Rounding: Math.ceil (-2.9) =-2

===================================================

BigDecimal B = new BigDecimal (9.655);

Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();

Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();

System.out.println ("f1=" + F1);//f1=9.65

BigDecimal mdata = new BigDecimal ("9.655"). Setscale (2, bigdecimal.round_half_up);

System.out.println ("mdata=" + mdata);//mdata=9.66

BigDecimal (Double val)
Will The double is converted to BigDecimal, which is the Exact decimal representation of a double binary floating-point value. The scale of the returned BigDecimal is the smallest value that makes (10scale xval) an integer.

Note:

  1. The result of this construction method is somewhat unpredictable. One might think of writing   in Java; new BigDecimal (0.1)   created   BigDecimal Span class= "Apple-converted-space" >  is exactly equal to 0.1 (non-scaling value 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as   double (or in that case, cannot be represented as a binary decimal of any finite length). In this way, the value of incoming to the construction method does not exactly equal 0.1 (although it is equal to the value on the surface).
  2. On the other hand, theString Construction method is fully predictable: writing new BigDecimal ("0.1") creates a BigDecimal, which Exactly equal to the expected 0.1. Therefore, in comparison, it is generally advisable to use the String construction method first.
  3. WhenDoublewhen you must use it as a source for BigDecimal, be Aware that this construction method provides an accurate conversion; it does not provide the same results as the following: Using the Double.toString(double) method First, and then using BigDecimal(String) the Constructs a method that converts a double to a String. To get the result, use the static valueOf(double) method.

Java rounding and Java round methods

Related Article

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.