4GetFullOf
4GetFullOf

Reputation: 1738

Float to integer conversion

I want to take the value from my slider and without rounding up, crop the decimals. So if I have a float value of 10.973. I want it in an integer as 10. I dont use negative values but if the slider value is between 0 and 1 id rather it round up. How would i make a statement doing the following?

Upvotes: 1

Views: 844

Answers (3)

Thomas Nguyen
Thomas Nguyen

Reputation: 494

A simple cast to integer should do:

int i = int(f)

If f is 10.973 i will be 10. If f is -10.973, i will be -10. A cast to int will simply truncate the decimal part.

Upvotes: 0

Jason Clardy
Jason Clardy

Reputation: 153

Just a simple integer cast would work.

int intVal = (int)floatVal;

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

You should use:

f = (int)floorf(x);

floorf however will round to the lower integer so -2.79 will result in -3.

Upvotes: 1

Related Questions