Help with printf

This is a discussion on Help with printf within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'm working on a very simple program to output some date in a table. The last column of the ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-27-2004
Tony
 
Posts: n/a
Default Help with printf

I'm working on a very simple program to output some date in a table. The
last column of the table shows the prices of various items. I'd like all of
these prices to line up at the decimal point. In light of that, the code
for the last column looks something like this (I've simplified it for this
example):

printf("<td> $ %5.2f </td>", $price);

When I do this, however, I don't actually get the desired results. I do get
2 digits of decimal precision, but I do not get an overall field width of 5.
What I wind up with is a column that looks like this:

$ 5.25
$ .10
$ 19.25

Can anyone suggest a solution to this problem?

I'm guessing (?) that this may have something to do with html ignoring white
space in certain contexts, but I'm not sure if that's the case. More
importantly, I'm not sure of an "easy" solution.

Tony


Reply With Quote
  #2 (permalink)  
Old 01-27-2004
php
 
Posts: n/a
Default Re: Help with printf

Generally browsers ignore white space when rendering.

Here are two options.

1) use preformatting tags
provided you use a mono-spaced font instead of a proportionally spaced- one
this will preserve the spaces and line feeds

(imagine the "b" represents a space character)
<pre>
bbbb$b5.25
bbbbb$b.10
bbb$b19.25
</pre>

2) use tables

<table>
<tr><td align='right'>$ 5.25
<tr><td align='right'>$ .10
<tr><td align='right'>$ 19.25
</table>


Good Luck

"Tony" <nospam@pittarese.com> wrote in message
news:bv4h0r$glm@library2.airnews.net...
> I'm working on a very simple program to output some date in a table. The
> last column of the table shows the prices of various items. I'd like all

of
> these prices to line up at the decimal point. In light of that, the code
> for the last column looks something like this (I've simplified it for this
> example):
>
> printf("<td> $ %5.2f </td>", $price);
>
> When I do this, however, I don't actually get the desired results. I do

get
> 2 digits of decimal precision, but I do not get an overall field width of

5.
> What I wind up with is a column that looks like this:
>
> $ 5.25
> $ .10
> $ 19.25
>
> Can anyone suggest a solution to this problem?
>
> I'm guessing (?) that this may have something to do with html ignoring

white
> space in certain contexts, but I'm not sure if that's the case. More
> importantly, I'm not sure of an "easy" solution.
>
> Tony
>
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0