Math.Round ()

Source: Internet
Author: User

Rounding in JAVA using the Math.Round (T-a) function, the function returns a long type, which can be double or float.

View JDK Source code:

  

public static long round (double a) {        if (a = 0x1.fffffffffffffp-2)//greatest Double value less than 0.5            return (long) floor (a + 0.5d);        else            return 0;}

  

public static double floor (double a) {        return Strictmath.floor (a),//default impl. Delegates to Strictmath}

  

public static double floor (double a) {     return Floororceil (A,-1.0, 0.0,-1.0);}

  

In fact, concrete implementation is quite complicated.

It is important to note that a is a negative case.

1System.out.println ("The first =5 after the decimal place");2System.out.println ("Positive: math.round (11.5) =" + Math.Round (11.5));3SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.5) =" + Math.Round (-11.5));4 System.out.println ();5System.out.println ("The first <5 after the decimal place");6System.out.println ("Positive: Math.Round (11.46) =" + Math.Round (11.46));7SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.46) =" + Math.Round (-11.46));8 System.out.println ();9System.out.println ("The first >5 after the decimal place");TenSystem.out.println ("Positive: Math.Round (11.68) =" + Math.Round (11.68)); OneSystem.out.println ("Negative: Math.Round ( -11.68) =" + Math.Round (-11.68));

The output is:

First digit after decimal point =5 positive: Math.Round (11.5) =12 negative: Math.Round (-11.5) =-11 first digit after decimal point <5  Positive number: Math.Round (11.46) =11 negative: Math.Round (-11.46) =-11 the first digit after the decimal point >5 positive: Math.Round ( 11.68) =12 negative: Math.Round (-11.68) =-12

Positive number and we usually learn the same, negative numbers in the decimal point after the first 5 o'clock, directly to the fractional portion, greater than 5 o'clock minus one, less than 5 o'clock directly to the fractional part.

Math.Round ()

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.