I am trying to echo an array as a price in php, if the price is for example £2.55 is comes up as £2.55, if it is £2.50 is comes up as £2.5 , how can I get it to add the zero on the end ?.
My Mysql DB field is currently set to decimal(10,2)
Many Thanks
🙂
Merry Christmas !
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
<?php
$query = mysql_query("SELECT * FROM hqfjt_chronoforms_data_dashboard WHERE cf_id = '1'") or die(mysql_error());
$oilprice = mysql_fetch_object($query);
$oilpricegasoil = $oilprice->oilpricegasoil;
$oilpricederv = $oilprice->oilpricederv;
$oilpricekero = $oilprice->oilpricekero;
?>
<tr><?php $gasoilprice = $oilpricegasoil + $row->gasoilmargin;
$dervprice = $oilpricederv + $row->dervmargin;
$keroprice = $oilpricekero + $row->keromargin;?>
<td><?php echo $row->leadname; ?></td>
<td><?php echo $row->businessname; ?></td>
<td><?php echo $row->postcode; ?></td>
<td><?php echo $row->gasoiluser; ?></td>
<td><?php echo $row->dervuser; ?></td>
<td><?php echo $row->kerouser; ?></td>
<td><?php echo '£'; echo $gasoilprice; ?></td>
<td><?php echo '£'; echo $dervprice; ?></td>
<td><?php echo '£'; echo $keroprice; ?></td>
Simple formatting
Try the
number_formatfunction:Be careful because this will round
.005to the next highest cent, which isn’t always what you want:If you need different rounding, try using:
round()ceil()floor()Money formatting
You could also try the
money_formatfunction, though you’ll need to make sure your locale is set correctly for it to work.Use a function
Either way, I recommend you push the conversion into a function, so you can reuse it throughout your code and don’t have to repeat your number formatting: