Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide Long Long Number as Percent

In an iphone app, I have 2 large numbers stored in NSStrings, and I want to figure out the float number that is achieved by dividing them.

Right now, I have:

unsigned long long number = [string1 longLongValue];
unsigned long long number2 = [string2 longLongValue];
float percent = number/number2;
[textField setText:[NSString stringWithFormat: @"%f%%",percent]];

(I assume I have to use "unsigned long long" instead of ints because the numbers in the NSStrings are pretty high- the first one is 309,681,754 and the second is 6,854,433,820)

However, after I do this, I always get 0% in the text field. What am I doing wrong?

Thanks for any help in advance.

like image 399
element119 Avatar asked Jul 07 '10 18:07

element119


People also ask

How do you find the percentage of a smaller number from a larger number?

Divide the smaller number by the bigger number, then times by 100 E.g. if I wanted to find out 3 as a percentage of 50 I'd do: 3 / 50 = 0.06 0.06 x 100 = 6 Therefore 3 is 6% of 50. Hope that helps!


1 Answers

You are dividing integers. That always results in an integer.

What you need to do is to cast them to floats before dividing. This should work:

float percent = (float)number / (float)number2;
like image 60
Zan Lynx Avatar answered Nov 11 '22 19:11

Zan Lynx