How to get a number to round to a whole number in php?

I have a part of my code that does calculations but it gives too many numbers behind the decimal sometimes.

How do I tell it to only produce whole numbers?
if($outcome == "Human wins") {
		$humanWins++;		
		$humanPercent = ($humanWins/$total) * 100;
	}
	else if($outcome == "Computer wins") {
		$computerWins++;	
		$computerPercent = ($computerWins/$total) * 100;
	}
	else if($outcome == "Tie") {
		$ties++;	
		$tiesPercent = ($ties/$total) * 100;
	}

Open in new window

FairyBusinessAsked:
Who is Participating?
 
jrm213jrm213Commented:
If you want to round to the nearest whole number use round function

$computerPercent = round(($computerWins/$total) * 100);

in this case (2/3) * 100 = 66.6666666667 it will round to 67
in this case (1/3) * 100 = 33.3333333333 it will round to 33

if you always want it to round up use ceil function

$computerPercent = ceil(($computerWins/$total) * 100);
in this case (2/3) * 100 = 66.6666666667 it will round to 67
in this case (1/3) * 100 = 33.3333333333 it will round to 34

if you always want it to round down use floor function
in this case (2/3) * 100 = 66.6666666667 it will round to 66
in this case (1/3) * 100 = 33.3333333333 it will round to 33
0
 
Chris StanyonCommented:
If you only want whole numbers, then the round() function would do it. It'll round up or down according to the value after the decimals.

If you want it to round up, use ceil()

If you want it to round down, use floor()


$humanPercent = round(($humanWins/$total) * 100);

Open in new window

0
 
FairyBusinessAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.