Domanda

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

È stato utile?

Soluzione

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;

Altri suggerimenti

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top