Click to See Complete Forum and Search --> : % in PHP


Raskii
03-27-2001, 11:36 AM
I'm in the process of building an (simple) online quiz and I want the score (percentage) to only show to two or three decimals. Right now I'm using modulus to try and shave off the extra numbers like this:


$studentScore = ($studentScore + .0005) - ($studentScore % .001);


This didn't do anything so I decided to use bcmod():


$studentScore = bcmod($studentScore + .0005,.001);


When I do that, the parser tells me that I'm trying to divide by 0!

Does modulus in PHP only handle integers? If so, how can I chop off the extra digits?

Salmon
03-27-2001, 12:08 PM
What about using round()?


$studentScore = round($studentScore, 2);

Stuka
03-27-2001, 12:13 PM
In most languages, modulo division (%) is an integer operation, which would explain your problems. Don't know PHP, but round() sounds like a good choice :).

Raskii
03-27-2001, 06:51 PM
Kewl, thanks!

Now that I've got a simple test working I want feedback :)

Anyone who wants to take it go to...
Drat, need to find the address...

OK, address is: http://www.hcweb.net/mikenune/Quiz.php .

Salmon
03-27-2001, 08:03 PM
[QUOTE]Now that I've got a simple test working I want feedback/QUOTE]

Looks like it works to me! Good job.