@fiat in case someone decides to give the UnitsNet package a try, there's the Information class that implements the FromBytes method, which allows you to convert from bytes to another unit, e.g. double result = Information.FromBytes(1547821).Megabytes; => this will return 1.547 (MB)., for example, sometimes i get values 50.9 MB which is good. but some other values are 37091 MB or 3082.86 MB, and i'd like values like that to be automatically converted to GB (37.09 GB, 3.08 GB respectively) if they are in the GB range. and if there are values that are not in MB range, they should be displayed in KB i.e. 0.78 MB should just be , I got this code to covert size in bytes via PHP. Now I want to convert those sizes to human readable sizes using JavaScript. I tried to convert this code to JavaScript, which looks like this: fun, The new term was MiB (mebibyte) was created to represent a megabyte being 1024 * 1024 bytes. Mebibyte = 2^20 = 1024 x 1024 = 1,048,576 bytes So now you ask: Gigabyte or Gibibyte (1000 or 1024)? The selected answer provides a good answer: The sad fact is that it depends on who you ask This answer, hopefully, helps explain why the answer depends., How can I convert the output of PHP's filesize() function to a nice format with MegaBytes, KiloBytes etc? like: if the size is less than 1 MB, show the size in KB if it's between 1 MB - 1 GB show , I have an attachments table that stores the size of the document in Bytes. I'm needing to display a result set of all documents in either KB or MB. In KB if the document is less than 1MB or in MB .