The following code:
<?php
header("Content-Type: text/plain");
$numbers = array(3.714, 0.7142857142857 143, 9.667, 6.333, 7, 6, 8, 2, 3,
-2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5, 6.857);
foreach ($numbers as $value) printf("% 20.2f\n", $value);
?>
produces the following output:
3.71
0.71
9.67
6.33
7.00
6.00
8.00
2.00
3.00
-2.00
0.00
-6.60
2.25
4.33
-8.00
3.00
3.14
5.50
6.50
6.86
It seems that the width specifier of 20 refers to the total width,
except for numbers less than 1, where it refers to the number of
characters before the decimal point. It doesn't matter whether space
padding is used or not, or whether I output to an html file using the
<pre> element. Is there a quick way of aligning all the numbers? I know
I could use "if" to test whether a number is less than 1 and then
specifying a width of 17 character.
Thanks for any help.
Paul
<?php
header("Content-Type: text/plain");
$numbers = array(3.714, 0.7142857142857 143, 9.667, 6.333, 7, 6, 8, 2, 3,
-2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5, 6.857);
foreach ($numbers as $value) printf("% 20.2f\n", $value);
?>
produces the following output:
3.71
0.71
9.67
6.33
7.00
6.00
8.00
2.00
3.00
-2.00
0.00
-6.60
2.25
4.33
-8.00
3.00
3.14
5.50
6.50
6.86
It seems that the width specifier of 20 refers to the total width,
except for numbers less than 1, where it refers to the number of
characters before the decimal point. It doesn't matter whether space
padding is used or not, or whether I output to an html file using the
<pre> element. Is there a quick way of aligning all the numbers? I know
I could use "if" to test whether a number is less than 1 and then
specifying a width of 17 character.
Thanks for any help.
Paul
Comment