Math. Round () Ceil floor

Source: Internet
Author: User

 

 

Math. Round (11.5) and so on? Math. Round (-11.5) and so on?

The math class provides three methods related to rounding: Ceil, floor, and round. The functions of these methods correspond to the meanings of their English names. For example, the English meaning of Ceil is the ceiling. This method indicates the rounded up, math. the result of Ceil (11.3) is 12, math. the result of Ceil (-11.3) is-11. The English meaning of floor is floor. This method indicates downgrading, math. the result of Ceil (11.6) is 11, math. the result of Ceil (-11.6) is-12. The most difficult to master is the round method, which indicates "Rounding" and the algorithm is math. floor (x + 0.5), which adds 0.5 to the original number and then rounds down. Therefore, math. the result of round (11.5) is 12, math. the result of round (-11.5) is-11.

 

  System.out.println(Math.round(-11.5)+" "+Math.floor(-11.5+0.5)); //-11  System.out.println(Math.round(-11.6)+" "+Math.floor(-11.6+0.5));//-12

 

 

 1         // 四舍五入 2         System.out.println(Math.round(11.4)); 3         System.out.println(Math.round(11.6)); 4         System.out.println(Math.round(-11.2)); 5         System.out.println(Math.round(-11.6)); 6         // 向上取整 7         System.out.println(Math.ceil(11.6)); 8         System.out.println(Math.ceil(-11.4)); 9         // 向下取整10         System.out.println(Math.floor(11.6));11         System.out.println(Math.floor(-11.4));

 

 

// 四舍五入        System.out.println(Math.round(11.4));//11        System.out.println(Math.round(11.5));//12        System.out.println(Math.round(-11.4));//-11        System.out.println(Math.round(-11.5)); //-11        System.out.println(Math.round(-11.6));//-12

 

Math. Round () Ceil floor

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.