Help with displaying a float value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DudeBori82
Forum Commoner
Posts: 26
Joined: Thu Nov 18, 2004 10:09 am
Location: Florida

Help with displaying a float value

Post by DudeBori82 »

How do I limit the amount of decimal places when using 'echo' to display a float value. (It defaults to three I think, but I want it to display only two.) Please share with me your great wisdom and knowledge. :)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

How bout this

Post by neophyte »

Does this help?

Code: Select all

<?php
echo round(5.045, 2);  // echos 5.05
?>
DudeBori82
Forum Commoner
Posts: 26
Joined: Thu Nov 18, 2004 10:09 am
Location: Florida

Post by DudeBori82 »

thanx!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

not echo but if you

Code: Select all

<?php
$unitCost = 12.4456;
sprintf("%01.2f",$unitCost);
?>
will output 12.44
Post Reply