views:

585

answers:

5

I'm using a simple loop to echo back some numbers

<?php
$inc = 0.25;
$start =  0.25;
$stop = 5.00;
?>

<?php while($start != ($stop + $inc)){ ?>
<option><?php echo $start ?></option>
<?php $start = $start + $inc; ?>
<?php } ?>

However 5.00 appears as 5 and 4.50 appears as 4.5

How would i get this script to display 5.00, 4.00, 3.00, 3.50

Also apoligise for the title, i don't know how to explain this!

Thanks

+4  A: 

use this:

echo sprintf("%01.2f", $start)
martin.malek
Thank you very much.
dotty
+1  A: 

You'll want to use printf() for formatted strings:

printf("%01.2f", $start)

The full manual for (s)printf is here

iAn
+1  A: 
<?php while($start != ($stop + $inc)){ ?>
<option><?php printf('%01.2f', $start) ?></option>
<?php $start = $start + $inc; ?>
<?php } ?>
w35l3y
+2  A: 

There also is number_format that lets you choose the thousand and decimal separators: http://de.php.net/manual/en/function.number-format.php

KiNgMaR
A: 

Check out the bcmath library.

Robert L