How can I divide the small number by the large number?

I want to calculate difference over storage usage. I'm looking for the difference between the number A and the number B.

I'm using a simulator

A Number = 106581282816 -> Int64 (freeDiskSpaceInBytes)

B Number = 250790436864 -> Int64 (totalDiskSpaceInBytes)

When I use the calculator, the result is 0.42 as I want. -> A / B

When I calculate in Swift, the result is 0 and I couldn't find a solution. How can I solve this?

Here is the code I print to the console:

print(UIDevice.current.freeDiskSpaceInBytes / UIDevice.current.totalDiskSpaceInBytes) --> Result: 0

I am using the following extension.

  // MARK: Get String Value

  var totalDiskSpaceInGB: String {
    return ByteCountFormatter.string(fromByteCount: totalDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
  }

  var freeDiskSpaceInGB: String {
    return ByteCountFormatter.string(fromByteCount: freeDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
  }

  var usedDiskSpaceInGB: String {
    return ByteCountFormatter.string(fromByteCount: usedDiskSpaceInBytes, countStyle: ByteCountFormatter.CountStyle.decimal)
  }

  // MARK: Get raw value

  var totalDiskSpaceInBytes: Int64 {
    guard let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
       let space = (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value else { return 0 }
    return space
  }
  var freeDiskSpaceInBytes: Int64 {
    if #available(iOS 11.0, *) {
      if let space = try? URL(fileURLWithPath: NSHomeDirectory() as String).resourceValues(forKeys: [URLResourceKey.volumeAvailableCapacityForImportantUsageKey]).volumeAvailableCapacityForImportantUsage {
        return space
      } else {
        return 0
      }
    } else {
      if let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String),
        let freeSpace = (systemAttributes[FileAttributeKey.systemFreeSize] as? NSNumber)?.int64Value {
        return freeSpace
      } else {
        return 0
      }
    }
  }

  var usedDiskSpaceInBytes: Int64 {
    return totalDiskSpaceInBytes - freeDiskSpaceInBytes
  }
}
Answered by ForumsContributor in

I just stumbled upon this question while I was looking for something else, and I am making the assumption you've only recently started programming (apologies if not, but then you probably wouldn't have asked) ....

Ask yourself why would dividing 2 Ints yield 0?

Also perhaps, It's interesting that the Swift documentation doesn't often discuss the fundamental building blocks of all programming languages. It does assume the programmer already has that in their arsenal before launching themselves into the ether.

Here's a page (i found in a quick google search) that might explain what you need to know just as well as anyone here could...

[https://www.programiz.com/swift-programming/data-types]

Accepted Answer

It is logical: you get an Int64, hence A/B is 0.

But if you compute:

print(1000*aNumber/bNumber)

Then you get 424 And now divide (as Float) by 1000

let cNumber = 1000*aNumber/bNumber  // Int64
print(Float(cNumber) / 1000)

and get 0.424

You can also go to Double:

let aNumber : Int64 = 106581282816 // -> Int64 (freeDiskSpaceInBytes)
let bNumber : Int64 = 250790436864 // -> Int64 (totalDiskSpaceInBytes)

let c = Double(aNumber) / Double(bNumber)
print(c)

yields 0.42498144725429654

How can I divide the small number by the large number?
 
 
Q