Jump to content


Photo

A math function


  • Please log in to reply
1 reply to this topic

#1 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 12 August 2006 - 03:10 PM

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.

<?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;
}
?>

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 12 August 2006 - 03:46 PM

try
<?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);
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users