Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round down a double to the nearest smaller int in C?

Tags:

c

int

double

I've got a double:

double d = 25.342;

How can I convert it to 25 value?

If it were -12.46 I'd like to get -13.

like image 590
Manuel Araoz Avatar asked Oct 27 '09 15:10

Manuel Araoz


People also ask

How do you round a double to the nearest integer?

round() Math. round() accepts a double value and converts it into the nearest long value by adding 0.5 to the value and truncating its decimal points. The long value can then be converted to an int using typecasting.

Does double to int round down?

That is, the answer is always rounding down.

How do I round down in C?

In the C Programming Language, the floor function returns the largest integer that is smaller than or equal to x (ie: rounds downs the nearest integer).

In which rounding mode a number is always rounded to the smaller integer?

Randomized rounding to an integer All others are rounded to the closest integer. Whenever the fractional part is 0.5, alternate rounding up or down: for the first occurrence of a 0.5 fractional part, round up, for the second occurrence, round down, and so on.


1 Answers

int i = (int)floor(25.342);
like image 132
Khaled Alshaya Avatar answered Sep 18 '22 12:09

Khaled Alshaya