相关疑难解决方法(0)

round()for C++中的float

我需要一个简单的浮点舍入函数,因此:

double round(double);

round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1
Run Code Online (Sandbox Code Playgroud)

我能找到ceil()floor()在math.h中-但不是round().

它是以另一个名称存在于标准C++库中,还是缺少?

c++ floating-point rounding

227
推荐指数
11
解决办法
36万
查看次数

舍入整数除法(而不是截断)

我很想知道如何将数字舍入到最接近的第十个整数.例如,如果我有:

int a = 59 / 4;
Run Code Online (Sandbox Code Playgroud)

以浮点计算的14.75; 如何将数字存储为"a"中的15?

c math int rounding integer-division

67
推荐指数
9
解决办法
13万
查看次数

如何在C中舍入一个数字?

我尝试在 Stack Overflow 上搜索,但找不到答案。

这是代码:

#include <stdio.h>

int main(void) {
 

double y;
printf("Enter a number: ");
scanf("%lf", &y);
printf("Your number when rounded is: %.2lf", y); 
//If user inputs 5.05286, how can i round off this number so as to get the output as 5.00
//I want the output to be rounded as well as to be 2 decimal places like 10.6789 becomes 11.00


return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想对一个数字进行四舍五入,例如,如果数字是5.05286,则应四舍五入为5.00,如果是5.678901,则将四舍五入到6.002小数位。数字5.678901正在四舍五入,5.05 …

c rounding

0
推荐指数
1
解决办法
262
查看次数

标签 统计

rounding ×3

c ×2

c++ ×1

floating-point ×1

int ×1

integer-division ×1

math ×1