Divide NSUInteger by 100

860 Views Asked by At

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

2

There are 2 best solutions below

1
On BEST ANSWER

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;
0
On

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;