Question

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?

Was it helpful?

Solution

You should use:

f = (int)floorf(x);

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

OTHER TIPS

Just a simple integer cast would work.

int intVal = (int)floatVal;

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top