-1

I am using the PHP function https://stackoverflow.com/a/2510459/425964 written by @Mef https://stackoverflow.com/users/227532/mef to format bytes to kilobytes, megabytes, gigabytes, etc.

The problem is that:

echo formatBytes(9287695, 2);

Returns 8.86 MB. Should'nt it be 9.3 MB? Am I missing something here?

Thanks.

Community
  • 1
  • 1
Justin
  • 42,716
  • 77
  • 201
  • 296

4 Answers4

2

It shouldn't be 9.3 MB, the bytes always result in less when converted to KB / MB etc, since it's multiplied by 1024.

So if byte number starts with 92.. it will never result in 9.3 MB

I suppose 8.86 is the correct value.

Anonymous
  • 4,692
  • 8
  • 61
  • 91
1

9287695/1024/1024=8.86
9287695/1000/1000=9.30
1 MB = 1024 KB = 1024*1024 bytes

0

Units of Memory

Bit=a 1 or 0  
Byte=8 Bits  
Kilobyte=1024 Bytes  
Megabyte=1024 Kilobytes  
Gigabyte=1024 Megabytes  
Terabyte=1024 Gigabytes

So 9287695 Bytes is infact 8.86 MB

Brighton Vino
  • 295
  • 3
  • 10
0

There is a lot of confussion in IT world about distinguish Mebi* and Mega*, Kibi* and kilo*. You are actually perfectly right, as proper suffix for 8.86 should me "MiB" and not "MB".

It is because 1 MiB = 1024 KiB and 1 KiB = 1024 B. But 1 MB = 1000 kB and 1 kB = 1000 B.

Thus confusion.

So 8.86 is OK ((9287695/1024)/1024 = 8,857) as long as it is 8.86 MiB.

Plese refer here for more information: http://en.wikipedia.org/wiki/Mebibyte

Kuba Wyrostek
  • 6,163
  • 1
  • 22
  • 40