Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does casting double to int returns the round down number?

I have the folowing example :

double x = 2.5;
int n = (int)x;
int k = (int) floor(x);

Does casting double to int returns the round down number? or I should use floor function?

like image 353
Anis_Stack Avatar asked Feb 12 '16 15:02

Anis_Stack


Video Answer


1 Answers

Be careful with negative numbers. Casting will truncate towards 0. floor will truncate towards negative infinite.

If the value is positive, then both methods return the same truncated value.

like image 102
user3813674 Avatar answered Sep 18 '22 23:09

user3813674