go - golang printf float no leading zero -


http://play.golang.org/p/3mjfdttoxg

printf int ok, float, got no leading zero.

fmt.printf("%03.6f\n", 1.234) >1.234000 

why happen ?how make leading 0 display ?

golang v1.4.1

edit

i figured out

fmt.printf("%010.6f\n", 1.234) 

this ok now.

edit

from https://golang.org/pkg/fmt/

width , precision measured in units of unicode code points, is, runes. (this differs c's printf units measured in bytes.) either or both of flags may replaced character '*', causing values obtained next operand, must of type int.

the reason set 6 precision of string printed , 3 width. fmt package.

width specified optional decimal number following verb. if absent, width whatever necessary represent value. precision specified after (optional) width period followed decimal number. if no period present, default precision used.

...

0 pad leading zeros rather spaces;
numbers, moves padding after sign

thus 1.234000. leading 0s need increase width of string past 8 (length of 1 + . + 234000). %09.6f you'll 01.234000


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -