计算下载完成百分比时出错

时间:2012-04-27 16:54:43

标签: ios xcode

我有以下代码来计算下载文件的剩余百分比和剩余时间。我有来自chilkat FTP2模块的以下数据:字节/秒和剩余的文件总大小和文件大小。

有什么问题?我仍然得到高负数百分比和负数或非常小的时间(0.00213)

int Calcbytesreceived = [ftp.AsyncBytesReceived intValue];

int Calcdownloadrate = [ftp.DownloadRate intValue];

// data from FTP chilkat module: fileSize (bytes) ,  ForCalcbytesreceived (bytes) , ForCalcdownloadrate ( bytes / sec ) all Int format

double PercentOfFile = ( (double)Calcbytesreceived / (double)fileSize) * 100 ; 

double SecondsRemaining = ((double)fileSize - (double)Calcbytesreceived) / (double)Calcdownloadrate ;

double MinutesRemaining = (double)SecondsRemaining / 60 ;

NSLog(@"percent loaded = %@", PercentOfFile); 

1 个答案:

答案 0 :(得分:5)

PercentOfFile的类型为double。你需要使用"%f"而不是"%@"显示双精度值。将您的最后一行更改为:

NSLog(@"percent loaded = %f", PercentOfFile); 

编辑:这是指向何时使用不同NSLog的良好资源的链接

http://www.cocoadev.com/index.pl?NSLog