|
|
|
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 | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Allows you to parse a deliniated string and put the individual fields in a SELECT option in a form Categories : HTML, PHP, Strings | | | How to display a PHP variable value from a selectbox without reloading the
page by merging PHP and Javascript variables. Categories : PHP, Java Script, Variables | | | A simple function to prevent undefined $_POST/$_GET/$_SESSION variable errors Categories : PHP, Variables, Errors and Logging | | | Using data from a string. Categories : PHP, Strings, CURL | | | function textwrap will wrap text to any desired width using <BR>\n as the default line break.
Default wrap width is 80 columns.
Categories : Strings, HTML and PHP, PHP | | | This functions compares the current PHP version with a
desired version. Because of the 3 tiered version system, a
direct compare of a string to phpversion() will not be
accurate. Categories : PHP Configuration, PHP, Variables | | | Adding dashes to credit card numbers Categories : Strings, Credit Cards, PHP | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | Customizable encoding and decoding strings with security. Categories : PHP, Strings, HTML and PHP | | | Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | |
|
|
|