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, j=2.1, k=2.5, m=2.9; 7System.out.println ("rounding off decimal rounding: math.floor (2) =" + (int) Math.floor (i)); 8System.out.println ("rounding off decimal rounding: math.floor (2.1) =" + (int) Math.floor (j)); 9System.out.println ("rounding off decimal rounding: math.floor (2.5) =" + (int) Math.floor (k)); TenSystem.out.println ("rounding off decimal rounding: math.floor (2.9) =" + (int) Math.floor (m));  one                                          a    /*This commented code does not properly implement rounded rounding - System.out.println ("rounded rounding: math.rint (2) =" + (int) math.rint (i)); - System.out.println (rounding rounding: math.rint (2.1) = "+ (int) math.rint (j)); the System.out.println (rounding rounding: math.rint (2.5) = "+ (int) math.rint (k)"); - System.out.println ("rounded rounding: math.rint (2.9) =" + (int) math.rint (m)); -     - System.out.println ("rounded rounding: (2) =" + new DecimalFormat ("0"). format (i));  + System.out.println (rounding rounding: (2.1) = "+ new decimalformat (" 0 "). format (i));  - System.out.println (rounding rounding: (2.5) = "+ new decimalformat (" 0 "). format (i));  + System.out.println (rounding rounding: (2.9) = "+ new decimalformat (" 0 "). format (i));  a    */  at     -System.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)); -  inSystem.out.println ("rounding: Math.ceil (2) =" + (int) Math.ceil (i));  -System.out.println ("rounding: Math.ceil (2.1) =" + (int) Math.ceil (j));  toSystem.out.println ("rounding: Math.ceil (2.5) =" + (int) Math.ceil (k));  +System.out.println ("rounding: Math.ceil (2.9) =" + (int) Math.ceil (m)); -  theSystem.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)); Panax NotoginsengSystem.out.println ("rounding out fractional 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));  aSystem.out.println ("rounded rounding: (-2.5) =" +NewBigDecimal (" -2.5"). Setscale (0, bigdecimal.round_half_up));  theSystem.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));  $System.out.println ("rounding: Math.ceil (-2.5) =" + (int) Math.ceil (-k));  -System.out.println ("rounding: Math.ceil (-2.9) =" + (int) Math.ceil (-m));  -    }  the}
1Take out the small? rounding: math.floor (2) =22Take out the small? rounding: math.floor (2.1) =23Take out the small? rounding: math.floor (2.5) =24Take out the small? rounding: math.floor (2.9) =25Rounding Round rounding: (2) =26Rounding Round rounding: (2.1) =27Rounding Round rounding: (2.5) =38Rounding Round rounding: (2.9) =39? whole: Math.ceil (2) =2Ten? whole: Math.ceil (2.1) =3 one? whole: Math.ceil (2.5) =3 a? whole: Math.ceil (2.9) =3 -Take out small? rounding: math.floor (-2) =-2 -Take out small? rounding: math.floor (-2.1) =-3 theTake out small? rounding: math.floor (-2.5) =-3 -Take out small? 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 +? whole: Math.ceil (-2) =-2 a? whole: Math.ceil (-2.1) =-2 at? whole: Math.ceil (-2.5) =-2 -? whole: Math.ceil (-2.9) =-2
View Code

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 DoubleConverted to BigDecimal, the latter is DoubleThe exact decimal representation of the binary floating-point Value. Returned by BigDecimalThe scale is to make (10scalexval)is the minimum value for the Integer.

Note:

  1. The result of this construction method is somewhat unpredictable. One might think that the BigDecimal created by writing new BigDecimal (0.1) in Java is exactly equal to 0.1 (non-scale value 1, with a scale of 1), but it actually equals 0.100000000000000 0055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as a double(or, for that case, it cannot be represented as any finite-length binary decimal). This way, the value passed into the construction method does not exactly equal 0.1 (although it is on the surface equal to the value).
  2. On the other hand, theString construction method is fully predictable: writing new BigDecimal ("0.1") creates a BigDecimal, which is exactly equals the expected 0.1. therefore, in comparison, it is generally advisable to use the String construction Method First.
  3. When a double must be used as a source for BigDecimal , be aware that this construction method provides an accurate conversion; it does not provide the same result as the following: first use the Double.toString(double) method, and then use the BigDecimal(String) constructor method to The double is converted to String. To get the result, use the static valueOf(double) method.

Java rounding and Java round 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.