Surprising API behaviour change for android.text.format.Formatter.formatFileSize

Raymond Chan
1 min readJul 13, 2018

While we are implementing a feature to display the cache size in app for users, we think that there should be a simple function in Android to convert bytes to readable format.

So what we got is

android.text.format.Formatter.formatFileSize(Context context, long bytes)

Looks good, it takes a Context object, a little bit annoying though. But then the interesting part comes up. When I deploy the app on a 7.1 device, it behaves as expected like 10,73,741,824 bytes is showing 1.0 GB, but later when my colleague testing on his 8.1 device, it shows something like 1.1GB

Then I check the source code immediately, after SDK 25, the method has changed. It used to divide the bytes by 1,024. From SDK 26 on wards, it divides the bytes by 1,000, which is not expected. What a surprise!

--

--