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.
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!
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With