Jump to content

A math function


pixy

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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.