Quick question:
I am making a votting booth and when i print my percentages out it give me a heck of alot of deciaml places.. i am using echo to echo the variable out. is there any way to round up/down or strip off the extra decimal places..
Thanx!
Phil,
Couple of ways to trim your decimal points. First, the sledgehammer. There's a directive in php.ini called "precision" which is set to 14 by default. This represents the number of significant digits to be displayed in any double or float number. You can reduce your decimal points by modifying this value. Next, there's a rounding function, convienently called round(), which accepts to arguments... the number (double) to round and the precision (number of decimal places) to round it to. Finally, you can use number_format() to chop the number down to specified number of decimal points. Below are the appropriate links to the PHP Manual:
http://www.php.net/manual/en/function.round.php http://www.php.net/manual/en/function.number-format.php
HTH.
Cheers,
Geoff A. Virgo
Heres what I use on a poll feature to calculate the % to two dp.
$vote1per = round(($votes1 / $totalvotes)*100,2);
$vote1per = round(($votes1 / $totalvotes)*100,3); would be to 3dp and so on of course.