Bug 17459 – format("%012,3d", ...) doesn't handle field width and zero-padding correctly

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2017-06-01T17:28:29Z
Last change time
2017-12-18T22:57:31Z
Assigned to
No Owner
Creator
hsteoh

Comments

Comment #0 by hsteoh — 2017-06-01T17:28:29Z
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.
Comment #1 by github-bugzilla — 2017-12-09T17:30:41Z
Commit pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/9352627f89348a4479114a5bcdc64cf35c0957bf Fix for problems with format seperators fix issue 17459 fix issue 18047 This is in one commit as the two problem are located at the place andrei review
Comment #2 by github-bugzilla — 2017-12-18T22:57:31Z