0

I tried to convert file size from bytes to a human readable format. There's the hurry.filesize package, and another implementaion by Fred Cirera here:

Using hurry.filesize:

size(1024 ** 3.4)
Out[69]: '15G'

Using Fred Cirera:

sizeof_fmt(1024 ** 3.4)
Out[68]: '16.0GiB'

I am confused, so I try to calculate

1024 ** 0.4
Out[71]: 16.000000000000004

Why they got different result?

Community
  • 1
  • 1
Nick
  • 8,451
  • 13
  • 57
  • 106
  • `1024 ** 3.4` is `17179869183.999989`, divided by `1073741824` (one GB) is `15.99999999999999`. It's *almost* 16 GB but not quite, which is why the two may differ (one truncates and the other rounds, or something like that). – Some programmer dude Jun 16 '15 at 11:02
  • @JoachimPileborg Yes, you're right. From the `hurry.filesize` source code: `amount = int(bytes/factor)` - it is truncated. (Personally, I think it should not). Thank you! – Nick Jun 17 '15 at 01:50

0 Answers0