[Issue 17459] New: format("%012, 3d", ...) doesn't handle field width and zero-padding correctly

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jun 1 10:28:29 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17459

          Issue ID: 17459
           Summary: format("%012,3d", ...) doesn't handle field width and
                    zero-padding correctly
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: hsteoh at quickfur.ath.cx

Code:
--------
import std.stdio;
void main() {
        writefln("%012,3d", 100);
        writefln("%012,3d", 1_000);
        writefln("%012,3d", 100_000);
        writefln("%012,3d", 1_000_000);
        writefln("%012,3d", 100_000_000);
}
--------

Expected output:
--------
0000,000,100
0000,001,000
0000,100,000
0001,000,000
0100,000,000
--------

Actual output:
--------
00000000100
00000001,000
0000100,000
0001,000,000
100,000,000
--------

Note the uneven field widths in the output. In particular, output lines 1, 3, 5
are 1 character shorter than the specified field width of 12. If "%012d" were
used as format string instead, the output is correctly padded to 12 characters.

Also, it would be nice if the comma separators were also inserted into the zero
padding so that multiple values printed with the same format would have
separators nicely lined up.  But this point is more arguable, because it's not
clear what should happen if the separator falls on the first character of the
output, since printing ",000,000,100" for the first line would look rather
strange. But not printing the comma means the zero padding causes a 4-digit
grouping rather than the requested 3-digit grouping.  I'm not sure what's the
correct solution.

--


More information about the Digitalmars-d-bugs mailing list