convert float to int without rounding
-
How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578
-
How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578
-
int i = (int)(f + 0.5);
[
](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!
You expression rounds, which I think is the opposite of what he wants, which is simple truncation.
Software Zen:
delete this;
-
How would one convert a float to an int without rounding it? Currently: float f = 1.9; int i = (int)f; i would equal 2. How do I get it to not round, and have i equal 1 in the above example? Any help is appreciated. -Dev578
i
would not equal 2. Simple 'C'-style casts truncate, so you will seei == 1
.
Software Zen:
delete this;