在 Objective C 中计算下载百分比

发布于 2024-12-08 02:30:00 字数 349 浏览 0 评论 0原文

我遇到了一个看起来很简单但我无法解决的问题。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-12-15 02:30:00

要获得正确的答案,必须将值转换为 long double。

long double precent = ((long double)bytesDownloaded/(long double)downloadSize);
NSLog(@"precnt %Lf",precent);
NSLog(@"Downloadedbyret: %lld", bytesDownloaded);

To get the correct answer, you must cast the values to long double.

long double precent = ((long double)bytesDownloaded/(long double)downloadSize);
NSLog(@"precnt %Lf",precent);
NSLog(@"Downloadedbyret: %lld", bytesDownloaded);
甜味拾荒者 2024-12-15 02:30:00

分母是int吗,改成浮点数吧?尝试:

long double precent = (bytesDownloaded/(downloadSize * 1.0));
NSLog(@"precnt %Lf",precent); 
NSLog(@"Downloadedbyret: %lld", bytesDownloaded);

Is the denominator an int, change it to a floating point? Try:

long double precent = (bytesDownloaded/(downloadSize * 1.0));
NSLog(@"precnt %Lf",precent); 
NSLog(@"Downloadedbyret: %lld", bytesDownloaded);
夜吻♂芭芘 2024-12-15 02:30:00

要获得完成百分比,您需要乘以 100。

long double precent = (bytesDownloaded/downloadSize) * 100.0;

如果值 bytesDownloaded 和 downloadSize 是 int,您需要将它们转换为浮点值。

或者如果整数先将被除数乘以 100:

(bytesDownloaded * 100) / downloadSize;

To get % complete you need to multiply by 100.

long double precent = (bytesDownloaded/downloadSize) * 100.0;

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:

(bytesDownloaded * 100) / downloadSize;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文