在 Objective C 中计算下载百分比
我遇到了一个看起来很简单但我无法解决的问题。
我正在使用 Objective C 为 OSX 制作一个简单的下载管理器。作为应用程序的一部分,我试图计算当前下载的百分比。我正在尝试使用此代码,但它对我不起作用
long double precent = (bytesDownloaded/downloadSize) * 100;
NSLog(@"precnt %Lf",precent);
NSLog(@"Downloadedbyret: %lld", bytesDownloaded);
bytesDownloaded 和 downloadSize 很长。
有人可以请建议吗, 谢谢
I got stuck with a problem that looked pretty easy but i cant make it work.
I'm making a simple download manager for OSX using objective C. As part of the app im trying to calculate the percentage of the current download. im trying to use this code but it wont work for me
long double precent = (bytesDownloaded/downloadSize) * 100;
NSLog(@"precnt %Lf",precent);
NSLog(@"Downloadedbyret: %lld", bytesDownloaded);
The bytesDownloaded and downloadSize are long long.
Can someone please advise,
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获得正确的答案,必须将值转换为 long double。
To get the correct answer, you must cast the values to long double.
分母是int吗,改成浮点数吧?尝试:
Is the denominator an int, change it to a floating point? Try:
要获得完成百分比,您需要乘以 100。
如果值 bytesDownloaded 和 downloadSize 是 int,您需要将它们转换为浮点值。
或者如果整数先将被除数乘以 100:
To get % complete you need to multiply by 100.
if the values bytesDownloaded and downloadSize are int you will need to cast them to a floating point value.
Or if integers multiple the dividend by 100 first: