You can use the printf and sprintf functions to control the number of decimal places when
outputting numbers.
For example:
$double = 5.00;
// Will print 5
print $double . '<br>';
// Will print 5.00
printf ('%0.2f', $double);
$many_decimal_places = 5.000005;
// Will print 5.000005
print $many_decimal_places . '<br>';
// Will print 5.00
print sprintf ('%0.2f', $many_decimal_places);
Note:
AFAIK PHP stores all values - regardless of their type - as strings. If a number is not quoted,
then it is reduced to its most compact form (by stripping all leading and trailing zeros) before it
is used.
Try this example to see more clearly what happens:
print "005.00 evaluates to: " . 005.00 . '<br>';
print "5.00 evaluates to: " . 5.00 . '<br>';
print "'5.00' evaluates to: " .'5.00' . '<br><br>';
print "5 * 5 evaluates to: " . 5 * 5 . '<br>';
print "5.00 * 5.00 evaluates to: " . 5.00 * 5.00 . '<br>';
Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays , Filesystem , Variables , Strings , PHP Smart Strings and Echos Categories : PHP , Strings , Variables Working with files - return an array of files within a directory Categories : PHP , Strings , Variables , Filesystem Working with files - putting file contents to a string / var Categories : PHP , Filesystem , Variables , Strings What is the best way to split a string that consists of two bits of data
seperated by whitespace? Categories : Regexps , Strings , PHP Make old style (PHP3) scripts using GET, POST, COOKIE and File uploads (POST) compatible with
PHP 4.2.0 Categories : PHP , HTML and PHP , Global Variables , Cookies , Variables A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms , Variables , Arrays , PHP , PHP Classes Text Wrapping Categories : PHP , HTML and PHP , Strings How to pass an array to a function, or how to define a
function wich recieves an array. Categories : Variables , PHP , Arrays Allows you to parse a deliniated string and put the individual fields in a SELECT option in a form Categories : HTML , PHP , Strings Global Dump Highlighted Categories : PHP , Variables , Global Variables Functions that will format a date in either long or short format from a string. Categories : Date Time , Strings , PHP Produces browser-safe strings while preserving HTML tags. Categories : Strings , HTTP , PHP , HTML and PHP Pull deliniated text strings into a "SELECT" statement in a form. Categories : HTML and PHP , PHP , Strings serialize -- Generates a storable representation of a value Categories : PHP , PHP Functions , Variables