I'm trying to output columns of figures but they won't line up. It says in the tutorial I've got that fields are right-aligned by default; but my number fields are going silly. I'm getting the following...
124.00
5.00
23.00
etc.
My tutorial shows how to specify a field width for integers (I've checked that & everything lines up fine) and how to specify the number of decimal places for a floating point number. What it doesn't show, and I can't work out, is how to specify the TOTAL width of a floating point field with it right-aligned.
Any ideas?
Just to clarify:
You want something like this:
| Code: |
0124.00
0005.00
0023.00 |
correct?
yes, but without the leading zeroes. I just want the figures right-aligned in a fixed-width field.
Put the values in a table and right align the table cells. i.e.,
| Code: |
<td align="right">
23.00
</td> |
I'm trying to avoid tables. Apart from anything else if I do that I'll be dropping in & out of php all over the place & that would be a nightmare to follow.
You can try left padding the numbers with spaces ( ). It is not visible unlike a padded "0" but will serve the purpose.
| DoctorBeaver wrote: |
I'm trying to output columns of figures but they won't line up. [...]
Any ideas? |
Isn't simple HTML enough? | Code: |
<html><head>
<title>test</title>
<style type="text/css">
.master { width: 33%; }
.txt { width: 60%; float: left; }
.ra { width: 40%; text-align: right; float: right; }
</style>
</head>
<body>
<div class="master">
<div class="txt">bla bla bla<br>bla bla bla bla bla<br>bla bla bla bla</div>
<div class="ra">124.00<br>5.00<br>23.00</div>
</div>
</body>
</html> |
I'm constructing the page from data held in a mySQL database so 90% is written in php. The other 10% is a Jscript form. Categories are selected in the form and I list all items, prices, discounts, and other data underneath. The data comes from various tables using joins. Using tables to display the data would be a pain in the butt. There aren't even a fixed number of columns as some categories of item have more than others, the visitor may be entitled to a discount in 1 category but not another, and various other parameters.
I could have 2 items with 3 columns, 1 item with 2 columns, the next with 4 and so on. I don't know how many columns there needs to be for any given item until I read the database; so I want to format each value as it's displayed according to how many columns there are for that item, not be constrained by a table layout where I have to have the max number of columns there could possibly be only to find that some of them aren't needed.
As I mentioned before, it says in my tutorial that fields are right-aligned by default but this doesn't seem to be the case in practice. If they were it would save me a lot of trouble.
| DoctorBeaver wrote: |
| [...] it says in my tutorial that fields are right-aligned by default but this doesn't seem to be the case in practice. If they were it would save me a lot of trouble. |
| Code: |
<?php
$values = array(124, 5, 23);
echo '<pre>';
foreach ($values as $val) {
printf("%-23.23s | %10.2f\n", substr('abcdefghijklmnopqrstuvwxyz0123456789', 0, $val), $val);
}
echo '</pre>';
?> |
You said you use intergers.
Then don't use %f, which is for floats, but %d for integers
| Antoine_935 wrote: |
You said you use intergers.
Then don't use %f, which is for floats, but %d for integers |
Where did I say that? I can't see it anywhere. The only mention I made of integers was to say about the examples in the tutorial I've got.
| hexkid wrote: |
| Code: | <?php
$values = array(124, 5, 23);
echo '<pre>';
foreach ($values as $val) {
printf("%-23.23s | %10.2f\n", substr('abcdefghijklmnopqrstuvwxyz0123456789', 0, $val), $val);
}
echo '</pre>';
?> |
|
I tried that construct for a printf but it displayed ".2f"
However, I note that you've put "|" whereas I've been using ",". Could that make a difference?
The "|" or "," should not make any difference. The reason for your display of ".2f" must be something else ...
After tonight's sleep I realized what is your real problem
If you write, in HTML | Code: |
| XY Z <!-- 20 spaces --> |
your browser will display
So, if you build a nicely formatted table with printf()s | Code: |
N | Name | % | Country
---+---------+--------+----------
1 | hexkid | 24.55 | Portugal
14 | user2 | 0.13 | Germany
42 | Douglas | 42.00 | Krikkit
|
unless you wrap it in <pre>, it will be shown as | Quote: |
N | Name | % | Country
---+---------+--------+----------
1 | hexkid | 24.55 | Portugal
14 | user2 | 0.13 | Germany
42 | Douglas | 42.00 | Krikkit |
or even | Quote: |
| N | Name | % | Country ---+---------+--------+---------- 1 | hexkid | 24.55 | Portugal 14 | user2 | 0.13 | Germany 42 | Douglas | 42.00 | Krikkit |
I've got the whole thing wrapped in <pre></pre>
| DoctorBeaver wrote: |
| I've got the whole thing wrapped in <pre></pre> |
Post your code. If you have the page publicly available, post the URL too.