Formatted Output: Exact number of Decimal Places
Andrew Chamberlain via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 16 17:30:32 PDT 2016
On Monday, 16 May 2016 at 20:24:51 UTC, Q. Schroll wrote:
> Lets say I want to print a table with floats. How can it be
> formatted like that:
> | 2.4 |
> | 12.2 |
> | 8.131 |
> | 17.44 |
> Also acceptable is
> | 2.400 |
> | 12.200 |
> | 8.131 |
> | 17.440 |
> but not
> | 02.4 |
> | 12.2 |
> | 08.131 |
> | 17.44 |
> or any other solutions with leading zeros.
I have this one:
----
import std.stdio;
import std.string, std.algorithm, std.format;
void main(string[] args)
{
[2.4, 12.2, 8.131, 17.44].each!(a => format("%.3f", a)
.rightJustify(6).center(8).center(10,'|').writeln);
}
----
But only works if fractional and integral part are both up to 3
digits.
More information about the Digitalmars-d-learn
mailing list