Jump to content

Printf - Specifying Data Type, Precision, And Field Width At Once


chibineku
 Share

Recommended Posts

I would like to output a few things very precisely. I want to display this - gross: £20.00P&P: £4.95total: £24.95but have the colons of the left hand words all line up vertically, and have the £ signs of the numbers line up vertically, so basically the fields need to be right aligned and the numbers left aligned. I am trying this using PHP's printf function, which at the moment looks like:<?php printf("%7s%-7s", "gross:","£$gross");?>$gross is a number that has been floatval() -ed. I have set the white-space attribute of the list item elements in which these values reside, but without <pre></pre> tags, it doesnt display right. with them, it still displays wrong. Here is the live example without pre tags:without pre tagsAnd here is one with:with pre tagsI have used printf to good effect before, but it is baffling me this time. Any help would be great.

Link to comment
Share on other sites

Ha, you're right, it just feels wrong doing tables anymore...

Edited by chibineku
Link to comment
Share on other sites

You can also just float a couple divs, and align each one.<div style="width: 40px; text-align: right; float: left;">Gross:</div><div style="float: left; margin-left: 40px;">xxx</div><br style="clear: both;">I've used that style for various forms and things to get the labels to line up.

Link to comment
Share on other sites

True, I could do the div thing. I have used Synook's suggestion of a table already - it is tabular data, he's right, and it now lines up perfectly. I am also keen to make the site as accessible as possible and tables provide me with semantic elements that i can hide but which will be discoverable to screen readers so that users who browse differently know what's going on.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...