Welcome to PHPMule.com
A blog site which I am posting to during my learning of the popular programming language PHP. Feel free to leave comments!
-
PHP - Rounding Off Numbers
I am back to learning php as I have been away for a while working on projects but hey I’m back!
Rounding numbers in PHP is very easy as I needed to include this function in one of my scripts…
Let’s say we had the following number: 5.76. Like most people we don’t need to see the extra digits as it makes the web page look more messy. You may want to round numbers because of many reasons and it’s a good function to use when rounding money etc.
If we wanted to round 5.76 to the nearest integer (6) we would need to do the following.
Let’s begin with variable $number containing 5.76
$number = 5.76;
Then we use the following expression:
$number = round ($number);
Now what will happen is that variable $number will now contain the rounded off number so when you echo or print it back to the browser you will see the number “6″ displayed. That’s all to it!
You can also round the number to a specified decimal place, for example:
$number = round ($number, 2);
will output 5.8 to the browser!
(Post created on Wednesday, June 10th 09 at 22:40)
-
Archives
Recent Articles
- PHP - Rounding Off Numbers
- PHP - Single vs Double Quotation Marks
- PHP - Loading Files Into Your PHP Script By Using require() and include()
- PHP - Replacing Strings With Other Strings
- PHP - Finding Strings Within Strings
- PHP - Using strlen() To Test String Length
- PHP - Using The explode() Function
- PHP - Changing The String Case
- PHP - The nl2br() Function
- PHP - Trimming Strings
Links
Categories
- PHP (22)
- Uncategorized (1)


