Thread overview
Formatted Output: Exact number of Decimal Places
May 16, 2016
Q. Schroll
May 17, 2016
Marco Leise
May 17, 2016
Andrew Chamberlain
May 17, 2016
Ali Çehreli
May 16, 2016
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.
May 17, 2016
Am Mon, 16 May 2016 20:24:51 +0000
schrieb Q. Schroll <qs.il.paperinik@gmail.com>:

> 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 |

Use %#6.3f to get the above output.

> but not
> | 02.4   |
> | 12.2   |
> | 08.131 |
> | 17.44  |
> or any other solutions with leading zeros.

-- 
Marco

May 17, 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.
May 16, 2016
On 05/16/2016 01:24 PM, 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.

Also try "%.3g".

Ali