If you're only running on Windows you could use a p-invoke declaration in C# or VB.NET to call the Win32 functions StrFormatByteSizeW or StrFormatByteSize64. If your application/site is guaranteed to be running on at least Vista SP1 or Server 2008 there's also StrFormatByteSizeEx with a few more options.
Sample from the MSDN docs:
Numeric value Text string
532 532 bytes
1340 1.30KB
23506 22.9KB
2400016 2.29MB
2400000000 2.23GB
These APIs also handle localization correctly for non-English users.
divide the number by 1000000.0, then append an "m".
remember to round the number to 1 decimal place.
If you're only running on Windows you could use a p-invoke declaration in C# or VB.NET to call the Win32 functions StrFormatByteSizeW or StrFormatByteSize64. If your application/site is guaranteed to be running on at least Vista SP1 or Server 2008 there's also StrFormatByteSizeEx with a few more options.
Sample from the MSDN docs:
These APIs also handle localization correctly for non-English users.
This should output:
You can use Log10 to determine the correct break. Something like this could work: