C语言 C:如何使数字始终四舍五入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26444658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:29:10  来源:igfitidea点击:

C: How do I make a number always round up

cfunctionrounding

提问by centurion937

I'm wanting to make a function that calculates a cost based on time. For any time less than three hours, it would return a flat rate charge of $2. For each hour or part of an hour over that, it would charge an additional $0.50. Hours are input by user as a float data type to 2 decimal places. How can I make it to where it will always round up the time to a whole hour? Here's the code I have so far for the function:

我想做一个基于时间计算成本的函数。对于任何少于三个小时的时间,它将返回 2 美元的统一费率。每隔一小时或超过一小时,它将额外收取 0.50 美元。小时由用户作为浮点数据类型输入到小数点后两位。我怎样才能做到它总是将时间四舍五入到一个小时?这是我迄今为止为该函数编写的代码:

int calculateCharges(float hours)
{
 if (hours <= 3){
    return 2.00
}
else

回答by Frxstrem

First of all, you use the data type int, which cannot hold fractional values (and will implicitly round them towards zero, even before the function is ever called.) You should use doubleinstead, since this is the proper datatype for fractional numbers.

首先,您使用数据类型int,它不能保存小数值(并且会隐式地将它们舍入为零,甚至在函数被调用之前。)您应该double改用,因为这是小数的正确数据类型。

You would also want to use the ceil(x)function, which gives the nearest whole number larger than or equal to x.

您还需要使用该ceil(x)函数,该函数给出大于或等于 的最近整数x

#include <math.h>

double calculateCharges(double hours)
{
  if (hours <= 3) {
    return 2.00;
  } else {
    // return .00 for the first 3 hours,
    //  plus an additional 
#include <math.h>

double calculateCharge(double hours)
{ 
 //To use accurately the ceil function from Math, the parameter should be a double!
  double calculateTime = ( hours <3) ? 2 : (2+ceil(hours-3)*0.50); 
  return calculateTime;
}
.50 per hour begun after that return 2.00 + ceil(hours - 3) * 0.50; } }

回答by Kevin Avignon

To answer your query, you should study your variable type. You use hours as a in, but you return a 2.00 which is a floating number( aka not a integer)

要回答您的查询,您应该研究您的变量类型。您使用小时作为输入,但您返回一个 2.00,它是一个浮点数(又名不是整数)

##代码##

Include the math.h header to be able to use the Math struc. It is a really help structure when you have to compute values, you can calculate the sinus of a number for instance. In our situation, the ceil function will always return the value to round up to the nearest integer. You can learn more about it here : http://www.techonthenet.com/c_language/standard_library_functions/math_h/ceil.phpOh and also, you said less than three hours, but in your function you say less or equals to 3. Make sure you actually use the good operator or you could end up with results you are not expecting while running your function!

包含 math.h 头文件以便能够使用 Math 结构。当您必须计算值时,这是一个非常有用的结构,例如,您可以计算一个数字的正弦。在我们的情况下, ceil 函数将始终返回值以四舍五入到最接近的整数。你可以在这里了解更多信息:http: //www.techonthenet.com/c_language/standard_library_functions/math_h/ceil.php哦,还有,你说不到三个小时,但在你的函数中你说小于或等于 3。确保您确实使用了好的运算符,否则在运行函数时可能会得到意想不到的结果!

Oh, you can look up the ternary operator there in order to have a better understanding of it since it's a bit obscure the first time :http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

哦,你可以在那里查找三元运算符以便更好地理解它,因为它第一次有点模糊:http: //msdn.microsoft.com/en-us/library/ty67wk28.aspx

Hope it helps you :)

希望对你有帮助:)