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 a开发者_开发技巧nd 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.
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;
精彩评论