向上和向下舍入一个数字 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39925020/
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-08-28 14:52:12  来源:igfitidea点击:

Rounding up and down a number C++

c++cmathceil

提问by Teo Chuen Wei Bryan

I'm trying to allow my program to round a number up and down respectively.

我试图让我的程序分别向上和向下舍入一个数字。

For example, if the number is 3.6, my program is suppose to round up the nearest number which is 4 and if the number is 3.4, it will be rounded down to 3.

例如,如果数字是3.6,我的程序假设四舍五入最近的数字 4,如果数字是3.4,它将向下四舍五入为 3。

I tried using the ceillibrary to get the average of 3 items.

我尝试使用ceil库来获得 3 个项目的平均值。

results = ceil((marks1 + marks2 + marks3)/3)

results = ceil((marks1 + marks2 + marks3)/3)

However, the ceilonly rounds the number down but does not roll the number up.

但是,ceilonly 会将数字向下舍入但不会向上滚动。

There's 1 algorithm i stumbled upon

我偶然发现了 1 个算法

var roundedVal = Math.round(origVal*20)/20;

var roundedVal = Math.round(origVal*20)/20;

but i still can't figure a formula for some problem.

但我仍然无法找出解决某些问题的公式。

回答by brettmichaelgreen

std::ceil 

rounds up to the nearest integer

向上舍入到最接近的整数

std::floor 

rounds down to the nearest integer

向下舍入到最接近的整数

std::round 

performs the behavior you expect

执行您期望的行为

please give a use case with numbers if this does not provide you with what you need!

如果这不能为您提供您需要的东西,请给出一个带有数字的用例!

回答by Violet Giraffe

The function you need is called round, believe it or not.

你需要的函数叫做round,信不信由你。

ceilrounds UP, btw. That is, to the closest larger integer. floorrounds down.

ceil向上舍入,顺便说一句。也就是说,到最接近的较大整数。floor向下舍入。

回答by MarcD

You don't need a function to round in C or C++. You can just use a simple trick. Add 0.5 and then cast to an integer. That's probably all round does anyway.

您不需要在 C 或 C++ 中舍入的函数。你可以使用一个简单的技巧。添加 0.5,然后转换为整数。无论如何,这可能是全方位的。

double d = 3.1415;
double d2 = 4.7;
int i1 = (int)(d + 0.5);
int i2 = (int)(d2 + 0.5);

i1 is 3, and i2 is 5. You can verify it yourself.

i1是3,i2是5,你可以自己验证。

回答by u3885739

std::roundmay be the one you're looking for. However, bear in mind that it returns a float. You may want to try lroundor llroundto get a result in long or long long (C++ 11).

std::round可能就是你要找的人。但是,请记住它返回一个浮点数。您可能想要尝试lroundllround获得 long 或 long long (C++ 11) 的结果。

http://en.cppreference.com/w/cpp/numeric/math/round

http://en.cppreference.com/w/cpp/numeric/math/round

回答by Nishita Goyal

In c++, by including cmath library we can use use various functions which rounds off the value both up or down.

在 c++ 中,通过包含 cmath 库,我们可以使用各种函数来向上或向下舍入值。

std::trunc

This simply truncates the decimal part, thas is, the digits after the decimal point no matter what the decimal is.

这只是截断了小数部分,即小数点后的数字,无论小数是什么。

std::ceil

This is used to round up to the closest integer value.

这用于四舍五入到最接近的整数值。

std::floor

This is used to round down to the closest integer value.

这用于向下舍入到最接近的整数值。

std::round

This will round to the nearest integer value whichever is the closest, that is, it can be round up or round down.

这将四舍五入到最接近的整数值,即可以向上或向下四舍五入。