WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : How to control the number of decimal places when outputting numbers.
Categories : PHP, Strings, Variables Click here to Update Your Picture
Zak Greant
Date : Jun 16th 2000
Grade : 3 of 5 (graded 3 times)
Viewed : 7115
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Zak Greant
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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