How to convert byte size into human-readable format in Java? Like 1024 should become "1 Kb" and 1024*1024 should become "1 Mb".
I am kind of sick of writing this utility method for each project. Are there any static methods in Apache Commons for this?
Byte Units allows you to do it like this:
I have written another library called storage-units that allows you to do it like this:
In case you want to force a certain unit, do this:
This is a modified version of aioobe's answer.
Changes:
Locale
parameter, because some languages use.
and others,
as decimal point.I asked the same Question recently:
Format file size as MB, GB etc
While there is no out-of-the-box answer, I can live with the solution:
Test code:
Output (on my German Locale):
Edit: I have opened an Issue requesting this functionality for Google Guava. Perhaps someone would care to support it.