1

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
4GetFullOf
  • 1,738
  • 4
  • 22
  • 47

3 Answers3

1

You should use:

f = (int)floorf(x);

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

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

Just a simple integer cast would work.

int intVal = (int)floatVal;
Jason Clardy
  • 153
  • 6
0

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.

Thomas Nguyen
  • 464
  • 1
  • 5
  • 16