1. 程式人生 > >C語言(C++)中:詳解floor函式、ceil函式和round函式

C語言(C++)中:詳解floor函式、ceil函式和round函式

C語言中

1.floor函式

功能:把一個小數向下取整
      即就是如果數是2.2 ,那向下取整的結果就為2.000000
原型:double floor(doube x);
    引數解釋:
        x:是需要計算的數
返回值:
    成功:返回一個double型別的數,此數預設有6位小數
    無失敗的返回值
標頭檔案:#include<math.h>

示例
floor函式計算後的結果為double型別的:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() { double i = floor(2.2); double j = floor(-2.2); printf("The floor of 2.2 is %f\n", i); printf("The floor of 2.2 is %f\n", j); system("pause"); return 0; }

執行結果:
這裡寫圖片描述
floor函式把轉換後的結果強轉為int型別的:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() { int i = floor(2.2); int j = floor(2.7); printf("i=%d,j=%d\n", i, j); system("pause"); return 0; }

執行結果:
這裡寫圖片描述
ps:把計算結果強轉為int後,會丟失精度

2.ceil函式

功能:把一個小數向上取整
      即就是如果數是2.2 ,那向上取整的結果就為3.000000
原型:double ceil(doube x);
    引數解釋:
        x:是需要計算的數
返回值:
    成功:返回一個double型別的數,此數預設有6
位小數 無失敗的返回值 標頭檔案:#include<math.h>

示例
ceil函式計算的結果為double型別的:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    double i = ceil(2.2);
    double j = ceil(-2.2);
    printf("The ceil of 2.2 is %f\n", i);
    printf("The ceil of 2.2 is %f\n", j);
    system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述

ceil函式把計算後的結果強轉為int型別的:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    int i = ceil(2.2);
    int j = ceil(2.7);
    printf("i=%d,j=%d\n", i, j);
    system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述

3.round函式

功能:把一個小數四捨五入
      即就是如果數是2.2 ,那四捨五入的結果就為2
           如果數是2.5,那結果就是3
原型:double round(doube x);
    引數解釋:
        x:是需要計算的數
標頭檔案:#include<math.h>

示例

round函式的計算結果為double型別的:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    double i = round(2.2);
    double x = round(2.7);
    double j = round (-2.2);
    double y = round(-2.7);
    printf("The round of 2.2 is %f\n", i);
    printf("The round of 2.7 is %f\n", x);
    printf("The round of -2.2 is %f\n", j);
    printf("The round of -2.7 is %f\n", y);
    system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述

C++中

1.floor函式

#include<iostream>
using namespace std;
int main()
{
    double i = floor(2.2);
    double j = floor(-2.2);
    cout << "The floor of 2.2 is " << i << endl;
    cout << "The floor of -2.2 is " << j << endl;
    system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述

2.ceil函式

#include<iostream>
using namespace std;
int main()
{
    double i = ceil(2.2);
    double j = ceil(-2.2);
    cout << "The ceil of 2.2 is " << i << endl;
    cout << "The ceil of -2.2 is " << j << endl;
    system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述

3.round函式

#include<iostream>
using namespace std;
int main()
{
    double i = round(2.2);
    double x = round(2.7);
    double j = round(-2.2);
    double y = round(-2.7);
    cout << "The round of 2.2 is " << i << endl;
    cout << "The round of 2.7 is " << x << endl;
    cout << "The round of -2.2 is " << j << endl;
    cout << "The round of -2.7 is " << y << endl;
    system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述