Can you add an option to round half up/down? There are times when users want to see close approximation rather than safely rounded up sizes.
----- Original Message ---- From: Eric Blake <[email protected]> To: crocket <[email protected]> Cc: [email protected] Sent: Tue, January 11, 2011 5:43:35 AM Subject: Re: [coreutils] "ls -lh" rounds up the size of files instead of rounding half up/down. On 01/10/2011 01:30 PM, crocket wrote: > There is a file whose size is 42949837312bytes. > It's almost exactly 40.000153064727783GB. > "ls -lh" displays it as 41GB. > > It is resulted due to rounding up. > > However, rounding half down/up is much more common than rounding up in the > real > > > world. > When I expect moderate file size approximation, this 1GB difference is not > ignorable. > > Is there any reason for this behavior? Yes. -h is a GNU extension, but the comparable -k and -s options have behavior specified by POSIX. And POSIX requires that these options round up to the next integral number of units. Therefore, it is as if once you cross a unit boundary, ls is treating that unit as the block size and rounding up to the effective number of blocks of that unit size, since you've lost the fractional portion (that is, it's better to overestimate than underestimate when it comes to disk usage). http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html under STDOUT: "If any of the -l, -n, -s, [XSI] [Option Start] -g, or -o [Option End] options is specified, each list of files within the directory shall be preceded by a status line indicating the number of file system blocks occupied by files in the directory in 512-byte units if the -k option is not specified, or 1024-byte units if the -k option is specified, rounded up to the next integral number of units". > Or is it just a mistake? No, it is intentional. -- Eric Blake [email protected] +1-801-349-2682 Libvirt virtualization library http://libvirt.org
