Avatar of tonelm54
tonelm54
 asked on

Modulus but not full ints

Im trying to figure out how to modulus with decimal places.
$totTime = 1;
$modDivisor = 3 * 0.25;

echo "Total Time: " . $totTime . "<br/>";
echo "Divosor: " . $modDivisor . "<br/>";
echo $totTime % $modDivisor;

Open in new window


But keep getting

FATAL ERROR Uncaught DivisionByZeroError: Modulo by zero in /home4/phptest/public_html/code.php70(5) : eval()'d code:7 Stack trace: #0 /home4/phptest/public_html/code.php70(5): eval() #1 {main} thrown on line number 7

The issue I have is the "3*0.25" can be "2*0.25" or "5*0.25" or any other number, so I cant multiply into full number

For example:-
php
$totTime = 1;
$modDivisor = 5 * 0.25;

echo "Total Time: " . $totTime . "<br/>";
echo "Divosor: " . $modDivisor . "<br/>";
echo $totTime % $modDivisor;

Open in new window


Should give 0.25, but instead gives 0

Any ideas?
PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gr8gonzo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Julian Hansen

You could also do this
<?php
$totTime = 1;
$modDivisor = 5 * 25;

echo "Total Time: " . $totTime . "<br/>";
echo "Divosor: " . $modDivisor . "<br/>";
echo (($totTime*100) % $modDivisor)/100);

Open in new window


Just move the numbers into the integer domain by multiplying by 100
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy