Rounding up and down C ++ numbers

I am trying to allow my program to round a number up and down accordingly.

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

I tried to use the library ceilto get the average of 3 elements.

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

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

There 1 algorithm came across

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

but I still can not determine the formula for some problem.

+11
source share
5 answers
std::ceil 

rounded to the nearest integer

std::floor 

rounded to the nearest integer

std::round 

fulfills expected behavior

, , !

+16

round, , .

ceil , . . floor .

+6

std::round , . , float. lround llround, (++ 11).

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

+4

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 - 3, i2 - 5. .

+3

c++, cmath, , , .

std::trunc

, , .

std::ceil

.

std::floor

.

std::round

, , , .

-1

Source: https://habr.com/ru/post/1657100/


All Articles