0

I want to get percentage of download in label. I use this code:

cell.label?.text = "\( (CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)) * 100)" 

But in this case I get numbers of percentage after the point.

I tried to use different code but it not help:

cell.label?.text = "\( (Int(totalBytesWritten) / Int(totalBytesExpectedToWrite)) * 100)" 

How to get percentage like this 98% and not like this 98.5353457%?

  • `cell.label?.text = "\(Int( (totalBytesWritten/totalBytesExpectedToWrite) * 100))"`? – Larme Jul 17 '18 at 16:22
  • @Larme I get 0 when download start or 100 if download completed. Not numbers in progress. –  Jul 17 '18 at 17:31
  • This may be of use. https://stackoverflow.com/questions/24051314/precision-string-format-specifier-in-swift – Rowan Jones Jul 17 '18 at 18:21

2 Answers2

1

Try Int(CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite) * 100.0)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

Try this

let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
progressLabel.text = String(format: "%.0f%%", progress * 100)
vivekDas
  • 1,248
  • 8
  • 12