PHP sprintf and Padding

software development


Ok, next problem.

I have gotten my scripts working on DH, but I have one little formatting issue that can not figgure out. You can look at the code at

The problem is that I format the emails that are sent out in this page and I use %4.2f to specify how I want money amounts to appear. On my old system this would force spaces in front of the amount of money if the pre-decimal number had less that 4 numbers in it to make it 4 numbers wide. On DH tho the padding is ignored and it just lines up all the numers based on their size which makes things look messy.

UPDATE: What is even more interesting is that it respects the padding when it comes to s type items, but not when it comes to f type items.

Any thoughts on this would be appreciated.



Can’t recreate the problem here; floating sprintf format seems to right-justify automatically unless a minus sign is placed after the percent-sign, which you aint got.
A few troubleshooting suggestions:

  1. escape all of your dollar-sign characters (instead of just a “$”, make them “$”).
  2. 4.2f is almost certainly too small for your field widths (hint: the “4” is the total length, not just the number of digits prior to the mantissa). Try something like 8.2f
  3. lose the superfluous spaces in your format string; they’re unnecessary and may be the cause of your aggrovation.

If none of that works, then y’all need yourself something more than just a programmer - some sort of voodoo witch doctor would be in order.


After some searching, there was a change in PHP after 4.3.3. It was confirmed in their bugs log. It used to be that the number only refered to the padding before the decimal point, but since then it refers to the padding for the whole variable. So by adding 3 (representing the decimal point and two decimal places) to the number, making it 7.2f it now works just fine.

Just thought I would share incase anyone else has this problem.