複製鏈接
請複製以下鏈接發送給好友

round函數

(C語言中的函數)

鎖定
round函數,是C語言中的術語,返回一個指定格式的舍入整數,該整數將被舍入到最接近的整數。
中文名
round函數
外文名
The round function
書    寫
round()
所屬門類
C語言

round函數函數

round roundf roundl

round函數主體

#include <math.h>long double roundl(long double x);double round(double x);float roundf(float x);

round函數描述

The round functions will return a rounded integer in the specified format that will be rounded to the nearest integer regardless of the current rounding mode.
(round函數將返回一個指定格式的舍入整數,該整數將被舍入到最接近的整數)

round函數返回值

The rounded value.

round函數例子

ceil(x)返回不小於x的最小整數值(然後轉換為double型)。
floor(x)返回不大於x的最大整數值。
round(x)返回x的四捨五入整數值。
#include
#include
int main(int argc, const char *argv[])
{
float num = 1.4999;
printf("ceil(%f) is %f\n", num, ceil(num));
printf("floor(%f) is %f\n", num, floor(num));
printf("round(%f) is %f\n", num, round(num));
return 0;
}
編譯:$cc test.c -lm
執行:$./a.out
ceil(1.499900) is 2.000000
floor(1.499900) is 1.000000
round(1.499900) is 1.000000
Matlab中round()

round函數函數功能

round函數使用方法

B = round(A)
對數組A中每個元素朝最近的方向取整數部分,並返回與A同維的整數數組B,對於一個複數參量A,則分別對其實部和虛數朝最近的方向取整數部分,並返回一複數數據B。

round函數應用舉例

a = [-1.9, -0.2, 3.4, 5.6, 7.0, 2.4+3.6i]
a =
Columns 1 through 4
-1.9000 -0.2000 3.4000 5.6000
Columns 5 through 6
7.0000 2.4000 + 3.6000i
round(a)
ans =
Columns 1 through 4
-2.0000 0 3.0000 6.0000
Columns 5 through 6
7.0000 2.0000 + 4.0000i
[1] 
參考資料