Bug 11400 – Floating point numbers with fractional part printing alignment problem

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-10-31T05:54:29Z
Last change time
2019-12-06T17:34:28Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-10-31T05:54:29Z
void main() { import std.stdio; writefln("%2d", 5); writefln("%2d", -5); writefln("%3d", 5); writefln("%3d", 12); writefln("%3d", -12); writeln; writefln("%2.0f", 5.0); writefln("%2.0f", -5.0); writefln("%3.0f", 5.0); writefln("%3.0f", 12.0); writefln("%3.0f", -12.0); writeln; writefln("%2.1f", 5.0); writefln("%2.1f", -5.0); writefln("%3.1f", 5.0); writefln("%3.1f", 12.0); writefln("%3.1f", -12.0); } dmd 2.064beta3 gives: 5 -5 5 12 -12 5 -5 5 12 -12 5.0 -5.0 5.0 12.0 -12.0 But I expect: 5 -5 5 12 -12 5 -5 5 12 -12 5.0 <== note here -5.0 5.0 <== note here 12.0 <== note here -12.0 Note that the problem is not present if you use ".0" when you print floating point numbers. This is useful to correct align a 2D matrix, to print it in a more readable way. You can see in this program: void main() { import std.stdio; double[][] m = [[4.243, 0.000, 0.000, 0.000], [5.185, 6.566, 0.000, 0.000], [12.728, 3.046, 1.650, 0.000], [9.899, 1.625, 1.850, 1.393]]; writefln("%(%(%2.3f %)\n%)", m); } That prints a table with misaligned columns: 4.243 0.000 0.000 0.000 5.185 6.566 0.000 0.000 12.728 3.046 1.650 0.000 9.899 1.625 1.850 1.393 that is a little harder to read than: 4.243 0.000 0.000 0.000 5.185 6.566 0.000 0.000 12.728 3.046 1.650 0.000 9.899 1.625 1.850 1.393
Comment #1 by bugzilla — 2019-12-06T17:34:28Z
You misunderstood the meaning of the number before the dot: When you've got "%2.3f", the 2 means the number of *all* symbols printed out, while 3 is the number of all digits after the dot. When you use "%6.3f" in your last example, everything is as expected...