开发者

Divide Long Long Number as Percent

开发者 https://www.devze.com 2023-01-06 19:50 出处:网络
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.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消