# A math function

## Recommended Posts

I've been having all sorts of trouble with this function. It's giving me huge values, negative numbers, etc.

It's for converting a money value into it's respective parts. I am making one with galleons, sickles and knuts. This is how they convert:

1 Galleon = 493 Knuts
1 Galleon = 17 Sickles
1 Sickle = 29 Knuts

I'm converting from knuts into all the other units. So if I had 235354351521 knuts, I want it to tell me know many galleons, sickles, and knuts that makes.

[code]
<?php
function money(\$cash) {
// First, find out galleons
\$galleons = round((\$cash / 493), 0);
\$remain = \$cash - (\$galleons * 493);
\$sickles = round((\$remain / 29), 0);
\$knuts = round((\$remain - (\$sickles * 29)), 0);
if (\$galleons !== 1) {
\$galleons = \$galleons .' Galleons';
}
else {
\$galleons = \$galleons .' Galleon';
}
if (\$sickles !== 1) {
\$sickles = \$sickles .' Sickles';
}
else {
\$sickles = \$sickles .' Sickle';
}
if (\$knuts !== 1) {
\$knuts = \$knuts .' Knuts';
}
else {
\$knuts = \$knuts .' Knut';
}
\$money = \$galleons .', '. \$sickles .', '. \$knuts;
return \$money;
}
?>[/code]
##### Share on other sites

try[code]
<?php
function money(\$cash) {
\$knuts = \$cash % 29;
if (\$knuts) \$knuts = "\$knuts Knut" . ((\$knuts == 1) ? '':'s'); else \$knuts = '';
\$cash = intval(\$cash / 29);
\$sickles = \$cash % 17;
if (\$sickles) \$sickles = "\$sickles Sickle" . ((\$sickles == 1) ? '':'s').', '; else \$sickles = '';
\$galon = intval(\$cash / 17);
if (\$galon) \$galon = "\$galon Galon" . ((\$galon == 1) ? '':'s').', '; else \$galon = '';
return \$galon.\$sickles.\$knuts;
}
echo money(123456789);
?>
[/code]
##### Share on other sites

This thread is more than a year old.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.