By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

Problem with numbers less than 1 when using printf to format floating-pointnumbers

P: n/a
The following code:

<?php
header("Content-Type: text/plain");
$numbers = array(3.714, 0.7142857142857143, 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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Paul wrote:
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.


What version of PHP are you using? Tried v4.3.4 and v4.3.9, printf()
works as documented.

---
Steve

Jul 17 '05 #2

P: n/a
Paul wrote:
The following code:

<?php
header("Content-Type: text/plain");
$numbers = array(3.714, 0.7142857142857143, 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

<snip>

For me (Linux, PHP 4.3.9) it produces the expected result.
Hmmm ... seems you're running PHP 4.3.7 :-)
http://bugs.php.net/bug.php?id=28633

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3

P: n/a
Steve wrote:
Paul wrote:
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.

What version of PHP are you using? Tried v4.3.4 and v4.3.9, printf()
works as documented.

---
Steve


4.3.8

I presume that "as documented" means that the width specifier refers to
the (minimum) number of characters in total and not those before the
decimal point. I'll now try installing another version of php on my
machine and see what happens.

Paul

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.