docker / go-units

Parse and print size and time units in human-readable format
https://godoc.org/github.com/docker/go-units
Apache License 2.0
218 stars 38 forks source link

Inconsistent decimal places #9

Open janten opened 8 years ago

janten commented 8 years ago

go-units removes trailing zeros in its output. This makes the output in Docker's progress indicators not line up.

Current:

cba48ac2c991: Downloading    23 MB/37.18 MB
f19be92d17a1: Downloading  24.3 MB/64.6 MB

Better:

cba48ac2c991: Downloading  23.0 MB/37.18 MB
f19be92d17a1: Downloading  24.3 MB/64.60 MB

One decimal place should be enough when dealing with sizes:

cba48ac2c991: Downloading  23.0 MB/37.2 MB
f19be92d17a1: Downloading  24.3 MB/64.6 MB
calavera commented 8 years ago

I agree, what do you think about opening a pull request? This should be fairly simple to fix :raised_hands:

boaz0 commented 8 years ago

If I may - fmt.Printf("%.4g\n", 10.0) prints 10, even though I was expecting to see 10.0. Reading the fmt godoc:

%g %e for large exponents, %f otherwise

gives the impression that I guessed correctly. So did I get the wrong idea or we need to open a ticket to the Golang team? Either way, I would like to work on that.

Thanks.

thaJeztah commented 8 years ago

Oh, I think I once suggested this https://github.com/docker/docker/issues/9977

boaz0 commented 8 years ago

From the fmt unit tests it looks like I got the wrong impression.

I guess we need to replace the "%.4g" format anyway.

If you don't mind, I will figure it out and post a PR in the next days.

Also (taken from https://golang.org/pkg/fmt/)

For floating-point values, width sets the minimum width of the field and precision sets the number of places after the decimal, if appropriate, except that for %g/%G it sets the total number of digits. For example, given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5. The default precision for %e and %f is 6; for %g it is the smallest number of digits necessary to identify the value uniquely.

janten commented 8 years ago

Thanks everybody for looking into this. I do currently not have the time to write a PR myself.