WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 14138
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  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
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