1

I have an NSUInteger and i want to divide it by 100. So lets say i have the number 1 in an NSUInteger i want to divide it by 100 and get 0.01 as the result.

(float) percent / 100

percent is an NSUInteger

Cristian
  • 61
  • 8
  • I agree with Max, the example provided appears syntactically correct although the result is ununsed. – Joe Aug 15 '12 at 21:53

2 Answers2

5

When dividing two integers, the result will be an integer. Make one of them a float (the constant is typical)

float percent = grade / 100.0f;
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
1

Your example looks fine since you are casting percent as a float. The rule of thumb to remember is to get a floating point value you have to divide by a floating point number. This means either both numbers are a floating point number or one of the numbers are floating point. Either of the following scenarios would solve your problem.

//I recommend the first option
float percentage = percent / 100.0f;
float percentage = (float)percent / 100.0f;
float percentage = (float)percent / 100;
Joe
  • 56,979
  • 9
  • 128
  • 135