www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17459] New: format("%012, 3d", ...) doesn't handle field width

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 puremagic.com
          Reporter: hsteoh 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.

--
Jun 01