Summary of the Floor,round and ceil of math

Source: Internet
Author: User
Tags integer

Floor returns the largest integer not greater than

Found is a 4-house 5-in calculation, in which is to be larger than its integer

Ceil is not less than his smallest integer

See examples

TD style= "BORDER:0;PADDING:0;" >1
  math.flo or math.round math.ceil
1 1 1.5 2
1.6 2 2
-2 -1 -1
-2 -1 -1
-2 -2 -1

The test procedure is as follows:

public class MyTest {
 public static void main(String[] args) {
  double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };
  for (double num : nums) {
   test(num);
  }
 }
 private static void test(double num) {
  System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
  System.out.println("Math.round(" + num + ")=" + Math.round(num));
  System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
 }
}

Run results

Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0

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.