www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Formatted Output: Exact number of Decimal Places

reply Q. Schroll <qs.il.paperinik gmail.com> writes:
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 16 2016
next sibling parent Marco Leise <Marco.Leise gmx.de> writes:
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 |
 but not
 | 02.4   |
 | 12.2   |
 | 08.131 |
 | 17.44  |
 or any other solutions with leading zeros.
-- Marco
May 16 2016
prev sibling next sibling parent Andrew Chamberlain <ChamAn gmx.nz> writes:
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
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
May 16 2016