Formatting file sizes – making bytes human readable in GWT

It’s one of those simple UI things that make a real difference.   You have to be pretty special to be able to instantly convert 45742364 bytes into 43.6MB in your head.

Enter some great code snippets I’ve discovered on StackOverflow that do this.  This four line wonder is top answer to that question (from Mr Ed.):

There’s another similar answer to this question, that can also produces binary units (from aioobe):

Unfortunately, neither of these will work on the client side of GWT.  String.format and DecimalFormat all give it a Sadface.

How do we make it work with GWT?

We drop in the excellent GWT NumberFormat. With very little effort we end up with this fully GWT compatible version:

Which is lovely.  Happyface once more.

Written by Tom

Leave a Reply