28

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.

I tried using the ceil library to get the average of 3 items.

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

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

There's 1 algorithm i stumbled upon

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

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

Bryan
  • 8,488
  • 14
  • 52
  • 78

5 Answers5

43
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!

11

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.

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.

Martin
  • 16,093
  • 1
  • 29
  • 48
MarcD
  • 588
  • 3
  • 11
  • 2
    Nice to know, but I prefer functions – Moritz Schmidt Jun 02 '22 at 10:46
  • @MoritzSchmidt wherever it is possible (unless it seriously hurts readability of code) it is best to avoid functions, as they imply a lot of overhead – Gregor Hartl Watters Oct 17 '22 at 01:20
  • @GregorWattersHärtl That's not true at all. They do not implay a "lot" of overhead. – Moritz Schmidt Oct 22 '22 at 19:23
  • @MoritzSchmidt that depends on your interpretation of "a lot". The same bit of code executed outside a function will have less overhead than the same code executed inside a function that is called (as long as the function has not been inlined), hence my suggestion to avoid function calls, where possible (especially inside loops). – Gregor Hartl Watters Oct 23 '22 at 22:41
  • 1
    @GregorWattersHärtl I'm pretty sure that in pretty much any production C++ application, performance degradation due to function calls is irrelevant. Unless you are implementing a library that needs to be highly performant, [readability should take precedence over performance](https://softwareengineering.stackexchange.com/questions/89620/clean-readable-code-vs-fast-hard-to-read-code-when-to-cross-the-line). Performance loss usually takes place in a different way. – Moritz Schmidt Oct 29 '22 at 15:30
  • 1
    @MoritzSchmidt there, I totally agree with you. Personally, I also prefer having my code look organised (using function calls), rather than just inlining the code, even though it might cost me a few fractions of a second :P – Gregor Hartl Watters Oct 29 '22 at 21:32
9

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

ceil rounds UP, btw. That is, to the closest larger integer. floor rounds down.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • I believe they worked the same way. I tried both `round` and `ceil` before. – Bryan Oct 07 '16 at 20:11
  • http://en.cppreference.com/w/cpp/numeric/math/round "Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode." There's `floor` which should do the trick. – 怀春춘 Oct 07 '16 at 20:11
  • @TeoChuenWeiBryan did you try them on both of your examples? – Code-Apprentice Oct 07 '16 at 20:15
  • @Code-Apprentice Yes. I tried it using this way `(ceil(1 + 3 + 6)/3)` but it seems to still round my number down instead of up. – Bryan Oct 07 '16 at 20:15
  • @TeoChuenWeiBryan why all the extra math. Test both functions with plain numbers to see the different behavior. – Code-Apprentice Oct 07 '16 at 20:17
  • @TeoChuenWeiBryan While you are at it, try `floor()`, too. – Code-Apprentice Oct 07 '16 at 20:18
5

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

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

怀春춘
  • 197
  • 5
1

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

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.

  • 2
    The other answer already mentions those functions (except `trunc`, which OP wasn't asking about). This answer doesn't seem to add any new information... – HolyBlackCat Oct 20 '18 at 12:07