PDA

Click to See Complete Forum and Search --> : PHP - Numbers to two decimal places?


Cheeko
Jul 29th, 2001, 01:46 AM
Hi,

I've made a script, it's a fruit machine. It takes away $1.00 from your account, and that it your balance. You then spin the reel. If you lose, it removes $0.20 from your balance. If you win, depending on the winning icon, you recieve $x.xx.

Now, I want the balance value to always be shown in two decimal places.. but sometimes it doesnt do this..

For example, when it is on $0.00, it shows up as: $00
Also, when it should be $15.00 it shows up as $150

All other times it shows up normally like $13.20 and $8.70

How do I make it so the numbers always go to 2 decimal places?

Cheers, Cheeko

scoutt
Jul 29th, 2001, 12:25 PM
can you supply some code?

denniswrenn
Jul 29th, 2001, 12:52 PM
$cash = 12.551;
$cash = number_format($cash, 2);
//$cash now equals 12.55

Cheeko
Jul 29th, 2001, 09:39 PM
<?
// Seed the random generator
srand((double)microtime()*1000000);

// Take his money
// Enter code removing Ð1.00 from his account in sql
if (!isset ($balance)) {
$balance = 1.00;
$win = 0.00;
}

if ($balance < 0.20) {
//Remove another Ð1.00 ;)
$balance = 1.00;
$win = 0.00;
$info = "After losing it all, You loaded another Ð1.00 into the slots..";
}

// Set our smilies ;)
$smilie_one = "http://www.3dpcgames.com/cwm/s/cwm/big/biggrin.gif";
$smilie_two = "http://www.mpz.co.uk/cwm/cwm/big/bigsmirk4.gif";
$smilie_three = "http://www.3dpcgames.com/cwm/s/cwm/big/cool.gif";
$smilie_four = "http://www.duhspot.com/users/smiley/s/cwm/big/disgust.gif";
$smilie_five = "http://www.contrabandent.com/pez/cwm/big/disturbed.gif";
$smilie_six = "http://www.mpz.co.uk/cwm/cwm/big/eek3.gif";
$smilie_seven = "http://www.duhspot.com/users/smiley/s/cwm/big/evil.gif";
$smilie_eight = "http://www.themelee.com/smilies/s/cwm/big/firemad.gif";

$scroller = "To play, hit the spin button and hope for the best!! More features coming soon!!";

function get_win_cash()
{

global $win;
global $balance;
global $smi_one;
global $smilie_one;

if ($smi_one == $smilie_one) {
$win = 10.00;
$balance = $balance + 0.20;
$balance = $balance + $win;
} elseif ($smi_one == $smilie_two) {
$win = 7.50;
$balance = $balance + 0.20;
$balance = $balance + $win;
} elseif ($smi_one == $smilie_three) {
$win = 5.00;
$balance = $balance + 0.20;
$balance = $balance + $win;
} elseif ($smi_one == $smilie_four) {
$win = 5.00;
$balance = $balance + 0.20;
$balance = $balance + $win;
} elseif ($smi_one == $smilie_five) {
$win = 3.50;
$balance = $balance + 0.20;
$balance = $balance + $win;
} elseif ($smi_one == $smilie_six) {
$win = 2.50;
$balance = $balance + 0.20;
$balance = $balance + $win;
} elseif ($smi_one == $smilie_seven) {
$win = 2.50;
$balance = $balance + 0.20;
$balance = $balance + $win;
} else {
$win = 1.00;
$balance = $balance + 0.2;
$balance = $balance + $win;
}

$win = number_format($win, 2);
$balance = number_format($balance, 2);

return $win;
return $balance;
}

// Smilie Subs

function get_win_smilies()
{
global $smilie_one;
global $smilie_two;
global $smilie_three;
global $smilie_four;
global $smilie_five;
global $smilie_six;
global $smilie_seven;
global $smilie_eight;

$win_smilie = rand(1,8);

if ($win_smilie == 1) {
$smi_one = "$smilie_one";
$smi_two = "$smilie_one";
$smi_three = "$smilie_one";
} elseif ($win_smilie == 2) {
$smi_one = "$smilie_two";
$smi_two = "$smilie_two";
$smi_three = "$smilie_two";
} elseif ($win_smilie == 3) {
$smi_one = "$smilie_three";
$smi_two = "$smilie_three";
$smi_three = "$smilie_three";
} elseif ($win_smilie == 4) {
$smi_one = "$smilie_four";
$smi_two = "$smilie_four";
$smi_three = "$smilie_four";
} elseif ($win_smilie == 5) {
$smi_one = "$smilie_five";
$smi_two = "$smilie_five";
$smi_three = "$smilie_five";
} elseif ($win_smilie == 6) {
$smi_one = "$smilie_six";
$smi_two = "$smilie_six";
$smi_three = "$smilie_six";
} elseif ($win_smilie == 7) {
$smi_one = "$smilie_seven";
$smi_two = "$smilie_seven";
$smi_three = "$smilie_seven";
} else {
$smi_one = "$smilie_eight";
$smi_two = "$smilie_eight";
$smi_three = "$smilie_eight";
}

echo "<td align=\"center\" valign=\"middle\"><img src=\"" . $smi_one . "\"></td><td align=\"center\" valign=\"middle\"><img src=\"" . $smi_two . "\"></td><td align=\"center\" valign=\"middle\"><img src=\"" . $smi_three . "\"></td>";
return $smi_one;
}

function get_lose_smilies()
{
global $smilie_one;
global $smilie_two;
global $smilie_three;
global $smilie_four;
global $smilie_five;
global $smilie_six;
global $smilie_seven;
global $smilie_eight;

$s1 = rand(1,8);
$s2 = rand(1,8);
$s3 = rand(1,8);

if ($s1 == $s2) {
if ($$s2 == $s3) {
$smi_two = $smilie_one;
$smi_three = $smilie_six;
}
}

if ($s1 == 1) {
$smi_one = "$smilie_one";
} elseif ($s1 == 2) {
$smi_one = "$smilie_two";
} elseif ($s1 == 3) {
$smi_one = "$smilie_three";
} elseif ($s1 == 4) {
$smi_one = "$smilie_four";
} elseif ($s1 == 5) {
$smi_one = "$smilie_five";
} elseif ($s1 == 6) {
$smi_one = "$smilie_six";
} elseif ($s1 == 7) {
$smi_one = "$smilie_seven";
} else {
$smi_one = "$smilie_eight";
}

if ($s2 == 1) {
$smi_two = "$smilie_one";
} elseif ($s2 == 2) {
$smi_two = "$smilie_two";
} elseif ($s2 == 3) {
$smi_two = "$smilie_three";
} elseif ($s2 == 4) {
$smi_two = "$smilie_four";
} elseif ($s2 == 5) {
$smi_two = "$smilie_five";
} elseif ($s2 == 6) {
$smi_two = "$smilie_six";
} elseif ($s2 == 7) {
$smi_two = "$smilie_seven";
} else {
$smi_two = "$smilie_eight";
}

if ($s3 == 1) {
$smi_three = "$smilie_one";
} elseif ($s3 == 2) {
$smi_three = "$smilie_two";
} elseif ($s3 == 3) {
$smi_three = "$smilie_three";
} elseif ($s3 == 4) {
$smi_three = "$smilie_four";
} elseif ($s3 == 5) {
$smi_three = "$smilie_five";
} elseif ($s3 == 6) {
$smi_three = "$smilie_six";
} elseif ($s3 == 7) {
$smi_three = "$smilie_seven";
} else {
$smi_three = "$smilie_eight";
}

echo "<td align=\"center\" valign=\"middle\"><img src=\"" . $smi_one . "\"></td><td align=\"center\" valign=\"middle\"><img src=\"" . $smi_two . "\"></td><td align=\"center\" valign=\"middle\"><img src=\"" . $smi_three . "\"></td>";
}
?>

<html>
<head>
<title>Smiley Fruit Machine</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#12ADDA" text="#000000" link="#F4F4F4" vlink="#DEDFDF" alink="#F4F4F4" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellspacing="0" cellpadding="3" align="center" height="100%">
<tr>
<td align="center" valign="middle"><br><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#F4F4F4"><? echo $info; ?></font><br>
<table width="50%" border="1" cellspacing="10" cellpadding="0" bordercolor="#F4F4F4" bgcolor="#000099">
<tr>
<td bgcolor="#12ADDA" colspan="3"><img src="header.jpg" width="350" height="140"></td>
</tr>
<tr bgcolor="#F4F4F4" align="center" valign="middle">
<td colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#000099"><marquee><? echo $scroller; ?></marquee></font></td>
</tr>
<tr>
<?
// Lets spin the reel..
$spin = rand(1,10);
$balance = $balance - 0.20;

// Lets see if our user won and what his winners were..
if ($spin == 2) {
get_win_smilies();
get_win_cash();
$result = "You Win Ð" . "$win";
} elseif ($spin == 4) {
get_win_smilies();
get_win_cash();
$result = "You Win Ð" . "$win";
} elseif ($spin == 7) {
get_win_smilies();
get_win_cash();
$result = "You Win Ð" . "$win";
} else {
$result = "You Lose!";
get_lose_smilies();
}
?>
</tr>
<tr>
<td align="center" valign="middle" colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="5" color="#F4F4F4"><? echo "$result"; ?> - You have Ð<? echo "$balance"; ?>0
</font></td>
</tr>
<tr align="center" valign="middle">
<td colspan="3">
<form name="Spin" method="post" action="<? echo "$PHP_SELF"; ?>">
<input type="hidden" name="balance" value="<? echo "$balance"; ?>">
<input type="submit" name="Submit" value="Spin The Reel">
</form>
</td>
</tr>
</table>
<br>
<table width="60%" border="1" cellspacing="0" cellpadding="5" align="center" bgcolor="000099" bordercolor="f4f4f4">
<tr>
<td height="49">
<div align="center">
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#F4F4F4">Copyright &copy; 2001 <a href="http://www.cheeko.co.uk/">cheeko.co.uk</a><br>
All Rights Reserved. All Images Stole The www Way.<br>
<a href="priv_pol.php">Privacy Policy</a></font></p>
</div>
</td>
</tr>
</table>
<br>
</td>
</tr>
</table>
</body>
</html>


Thats that code..

Even after using that number_format, i Still get 00 for 0.00 and when it wins instead of ebing say 17.50 its 17.500

Also, Im using Ð instead of $ ;) :D

scoutt
Jul 30th, 2001, 04:39 PM
well it worked fine for me. it had 15.00 on it but the only thing I noticed was that when I lost it would be like this 14.8, that is probably an internal php thing, kike the calculator how it take the last zero off. other than that it worked fine. good job.

I don't think you noticed but you have an extra zero in this line

<td align="center" valign="middle" colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="5" color="#F4F4F4"><? echo "$result"; ?> - You have Ð<? echo "$balance"; ?>0

Cheeko
Jul 31st, 2001, 07:11 PM
Hmm.. I didnt notice that! Cheers mate :)