NSMeasurement not converting between UnitInformationStorage Correctly

I'm trying to use (NS)Measurement to convert any higher unit into kilobytes.

let mes = Measurement(value: 3, unit: UnitInformationStorage.megabytes)

let kilobytes = mes.converted(to: .kilobytes).value 

I expect kilobytes to be 3072, but instead I get 3000.

I get that in computing we often do rounding and for many people 1000 kilobytes = 1MB when in reality it should be 1024 kilobytes = 1MB. Is there any way to change this behavior?

Accepted Reply

That’s the correct and documented behavior: the kilobytes and megabytes units are powers of 10. For powers of 2, use kibibytes and mebibytes.

Replies

That’s the correct and documented behavior: the kilobytes and megabytes units are powers of 10. For powers of 2, use kibibytes and mebibytes.

Also, if your goal is to render a quantity in a user-visible way, check out NSByteCountFormatter.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"