user393273
user393273

Reputation: 1438

Divide a integer and get decimal answer

i currently have an integer int i and its value is 60 i wish to divide this by 100 to get 0.60 but i cannot do this please help

int i = 60;
int x = 60 / 100; // Dosen't work should be 0.60 instead returns 0

Upvotes: 6

Views: 7313

Answers (3)

johnoodles
johnoodles

Reputation: 349

int i = 60;
float x = (float)i/100.0f;
NSLog(@"%.2lf", x);
//this will print 0.60

Upvotes: 1

Dan
Dan

Reputation: 17445

You can get the correct value using the following method:

float x = 60 / 100.0f;

If you are looking for the value 0.60 you cannot assign it to an integer (whole number) type.

Upvotes: 6

David Neiss
David Neiss

Reputation: 8237

divide by 100.0f and return result into a float.

Upvotes: 9

Related Questions