views:

46

answers:

1

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.

+4  A: 

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;
Zan Lynx
Thanks- that did the trick. If I wanted the result to be in percent format, how would I move the decimal point?
Chromium
For example, I'm getting .04518, and I want it to be 4.518%
Chromium
@golfromeo: Seriously? You are asking how to *move the decimal point*? Sigh. Multiply by 100.
Zan Lynx
Wow... here I am overthinking and thinking of trying to get an NSNumberFormatter method or something so I can change it...
Chromium