Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to PHP (Rounding Decimal Places) 1

Status
Not open for further replies.

SPYDERIX

Technical User
Jan 11, 2002
1,899
CA
Hi,

I'm new at PHP and need to know how to round to a certain number of decimal places.

For the script that I wrote I want the number to be rounded to 1 decimal place (86.7%) and not (86.66666666666666666).

# START CALCULATION
$percent = ($points/30)*100;
echo "\t\t\tYou got $percent%";
# END

How can I make this round to one decimal place?

Thanks!
greenjumpy.gif
NATE
design@spyderix-designz.com
 
Dave, Ya I'm starting out on my quest to learning Server-Side Scripting now. I've been holding off too long, and now's the time to get the ball rolling. I'm starting off with PHP and MySQL and then I will go to PERL afterwards.

For all those out there who need a break from it all, check out my little online quiz that I just made as my first PHP project to get a feel for PHP and if statements and writing and retrieving data from a file.


Glad to be here...finally...lol :)
greenjumpy.gif
NATE
design@spyderix-designz.com
 
LOL I know waaay too much useless stuff [lol]

Welcome to PHP and Tek-Tips. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Oh also if you are rounding numbers from mysql then you can include the round fuction within your mysql query instead of making PHP do all the work:

manual snippet:
---------------
ROUND(X)
Returns the argument X, rounded to the nearest integer:
mysql> SELECT ROUND(-1.23);
-> -1
mysql> SELECT ROUND(-1.58);
-> -2
mysql> SELECT ROUND(1.58);
-> 2

Note that the behaviour of ROUND() when the argument is half way between two integers depends on the C library implementation. Some round to the nearest even number, always up, always down, or always toward zero. If you need one kind of rounding, you should use a well-defined function like TRUNCATE() or FLOOR() instead.
ROUND(X,D)
Returns the argument X, rounded to a number with D decimals. If D is 0, the result will have no decimal point or fractional part:
mysql> SELECT ROUND(1.298, 1);
-> 1.3
mysql> SELECT ROUND(1.298, 0);
-> 1

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top