Rounding of small numbers in the Java language

Source: Internet
Author: User

1, double Java.lang.Math.floor (double A)

Returns the maximum double value (closest to the positive infinity direction) of an integer that is less than or equal to the parameter A in a mathematical sense, special case:

If the parameter value equals a mathematical integer, the returned result is the same as the parameter value

If the parameter is Nan (non-numeric) or infinity or +0 or-0, the return value is the same as the parameter value

Returns the largest (closest to positive infinity) value, is less double than or equal to the argument and was equal to A mathematical integer. Special cases:

If The argument value is already equal to a mathematical integer and then the result is the same as the argument.

If The argument is a NaN or an infinity or positive zero or negative zero and then the result is the same as the argument.

Math.floor (2.0) =2.0Math.floor (2.1) =2.0Math.floor (2.5) =2.0Math.floor (2.9) =2.0
Math.floor ( -2.0) =-2.0Math.floor ( -2.1) =-3.0Math.floor ( -2.5) =-3.0Math.floor ( -2.9) =-3.02.double Java.lang.Math.ceil (doublea)Returns the smallest double value (closest to the negative infinity direction) that is greater than or equal to the integer in a mathematical sense of the parameter A, special case:
if the parameter value is already equal to an integer in a mathematical sense, the returned result is the same as the parameterIf the argument is Nan (non-numeric) or infinity, or +0,-0, the return result is the same as the parameterif the parameter value is less than 0 or greater than -1.0

Returns the smallest (closest to NegativeInfinity) double value, is greater than or equalto the argument and is equal T o a mathematical integer. Specialcases:

    • If The argument value is already equal Toa mathematical integer and then the result is the same as theargument.
    • If The argument is a NaN or an infinity orpositive zero or negative zero and then the result is the same as theargument.
    • If The argument value is less than zerobut greater than-1.0 and then the result is Negativezero.
Math.ceil (2.0) =2.0Math.ceil (2.1) =3.0Math.ceil (2.5) =3.0Math.ceil (2.9) =3.0
Math.ceil ( -2.0) =-2.0Math.ceil ( -2.1) =-2.0Math.ceil ( -2.5) =-2.0Math.ceil ( -2.9) =-2.0
double Java.lang.Math.rint (doublea)

Returns the double value thatis closest in value to the argument and are equal to a mathematicalinteger. If double Mathematicalintegers is equally close and the result is the integer value of that iseven. Special cases:

    • If The argument value is already equal Toa mathematical integer and then the result is the same as theargument.
    • If The argument is a NaN or an infinity orpositive zero or negative zero and then the result is the same as theargument.
math.rint (2.0) =2.0Math.rint (2.5) =2.0Math.rint (2.6) =3.0Math.rint (2.9) =3.0
math.rint ( -2.0) =-2.0math.rint ( -2.5) =-2.0math.rint ( -2.6) =-3.0math.rint ( -2.9) =-3.0
long Java.lang.Math.round (Doublea)

Returns the closest long tothe argument. The result is rounded through adding 1/2,taking the floor of the "result" and casting the result to type long . In other words, the result is equal to the valueof the expression:

(long) Math.floor (A + 0.5d)

Special cases:

    • If The argument is a NaN, the result is 0.
    • If the argument is negative infinity or any value less than or equal Long.MIN_VALUE to the value of, the result was equal to the Val UE of Long.MIN_VALUE .
    • If The argument is positive infinity or any value greater than or equal Long.MAX_VALUE to the value of, the result was equal to the Value of Long.MAX_VALUE .
Math.Round (2.0) =2Math.Round (2.5) =3Math.Round (2.6) =3Math.Round (2.9) =3
Math.Round ( -2.0) =-2Math.Round ( -2.5) =-2Math.Round ( -2.6) =-3Math.Round ( -2.9) =-3

Rounding of small numbers in the Java language

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.