Import Java.math.BigDecimal;
Import Java.text.DecimalFormat;
public class testgetint{
public static void Main (string[] args) {
Double i=2, j=2.1, k=2.5, m=2.9;
System.out.println ("Rounding off Decimal rounding: Math.floor (2) =" + (int) Math.floor (i));
System.out.println ("Rounding off Decimal rounding: Math.floor (2.1) =" + (int) Math.floor (j));
System.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));
/* This commented code does not properly round 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));
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));
*/
System.out.println ("Rounded rounding: (2) =" + New BigDecimal ("2"). Setscale (0, bigdecimal.round_half_up));
System.out.println ("Rounded rounding: (2.1) =" + New BigDecimal ("2.1"). Setscale (0, bigdecimal.round_half_up));
System.out.println ("Rounded rounding: (2.5) =" + New BigDecimal ("2.5"). Setscale (0, bigdecimal.round_half_up));
System.out.println ("Rounded rounding: (2.9) =" + New BigDecimal ("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));
System.out.println ("Rounding out Fractional rounding: Math.floor ( -2) =" + (int) Math.floor (-i));
System.out.println ("Rounding off Decimal rounding: Math.floor ( -2.1) =" + (int) Math.floor (-j));
(" Drop Decimal Rounding: Math.floor ( -2.5) = "+ (int) Math.floor (-K)")
System.out.println ("Rounding out Decimal rounding: Math.floor (- 2.9) = "+ (int) Math.floor (-M)",
System.out.println ("Round Rounding: ( -2) =" + New BigDecimal ("2"). Setscale (0, bigdecimal.round_half_up));
System.out.println (Rounded rounding: (-2.1) = "+ New BigDecimal (" -2.1 "). Setscale (0, bigdecimal.round_half_up));
System.out.println (" Rounded rounding: ( -2.5) = "+ New BigDecimal (" -2.5 "). Setscale (0, bigdecimal.round_half_up));
System.out.println ("Rounded rounding: ( -2.9) =" + New BigDecimal (" -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));
}
}
//_____________________________
Take out the small? Rounding: Math.floor (2) =2
Take out the small? Rounding: Math.floor (2.1) =2
Take out the small? Rounding: Math.floor (2.5) =2
Take out the small? 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
? Whole: Math.ceil (2) =2
? Whole: Math.ceil (2.1) =3
? Whole: Math.ceil (2.5) =3
? Whole: Math.ceil (2.9) =3
Take out small? Rounding: Math.floor (-2) =-2
Take out small? Rounding: Math.floor (-2.1) =-3
Take 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
? Whole: Math.ceil (-2.1) =-2
? Whole: Math.ceil (-2.5) =-2
? Whole: 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 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:
- 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).
- On the other hand, theString construction method is fully predictable: writing new BigDecimal ("0.1") creates a BigDecimalthat exactly equals the expected 0.1. Therefore, in comparison, it is generally advisable to use the String construction method first.
- 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 from Dong Junjie