(.29 * 100) is not equal to 29. The former is a floating-point number, the latter is an integer. Remember, all floating-point numbers are an approximations of values, as many decimal numbers cannot be exactly represented in floating-point format.
The modulus operator, I believe, is only defined within PHP for integers, so the value (.29 * 100) must be converted to integer.
I figured it was something like that, but I tried using fmod and it made no difference. Is there a way to convert a number into an integer? I tried using floor(), but that didn't work either.
The problem, again, is the inexactness of floating-point variables.
In the case of (.29 * 100), the floor() function returns 28. ceil() will return 29.
ceil() works because PHP's value is slightly less than 29. But only in this case -- some other value may be slightly higher than the integer value and will return a number one too great.
You'll Laugh at me , but here goes. I am writing a converter for wizard gold . I solved this by using round()
$bob represents US Dollars.
1 Galleon =17 Sickles=493 Knuts.
Assumes 1 penny=1 knut.
Code:
<?
$bob=.29;
$knuts =round($bob*100);
$sickles = ($knuts/29);
$knuts = ($knuts % 29);
$galleons = ($sickles/17);
$sickles = ($sickles % 17);
print floor($galleons)." Galleons ".floor($sickles)." Sickles and ".floor($knuts)." Knuts<br>";
?>
Basically. I didn't want to grab it typograchically in case someone put in an odd value such as a half penny.
$bob =.295 for example. Rounding the product works. Thanks for helping me out.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.