masterofollies 01-08-2010, 07:04 PM I have variables that are made into number_format, how can I reverse it to remove the commas? I want to keep it on the variable, but I want to use the same variable a second time as a new variable and it not use commas.
Fou-Lu 01-08-2010, 07:09 PM You'll have to write a custom for that:
function reverse_number_format(&$num)
{
$num = (float)str_replace(',', '', $num);
}
The only thing you need to do is remove the commas, php will automatically interpret this new value as a number, so casting to a float is not necessary. Also, the function is only there so you can change a reference of the number, since str_replace returns the result of the call.
masterofollies 01-08-2010, 08:01 PM I believe I am doing this wrong.
if (isset($matches[0]) && is_array($matches[0]))
{
list($credits, $nightcredits, $taxcredits, $exptax, $pendexp, $pendcred, $ore, $fish, $clearing, $herbs, $weapon, $other) = $matches[0];
}
echo "<br /><br />Credits: $credits<br />Nightly Credits: $nightcredits<br />Taxed Credits: $taxcredits<br />Exp Tax: $exptax<br />Pending Exp: $pendexp<br />Pending Credits: $pendcred<br />Ore: $ore<br />Fresh Fish: $fish<br />Clearing: $clearing<br />Herbs: $herbs<br />Weapon/Armor: $weapon<br />Other Items: $other";
$credits2 = $credits;
$credits3 = $credits2 * 2;
$taxcred2 = $taxcredits;
$taxcred3 = $taxcred2 * 2;
$exptax2 = $exptax;
$exptax3 = $exptax2 * 2;
function reverse_number_format(&$num)
{
$credits3 = (float)str_replace(',', '', $credits3);
$taxcred3 = (float)str_replace(',', '', $taxcred3);
$exptax3 = (float)str_replace(',', '', $exptax3);
}
$final = $credits3 + $taxcred3 + $exptax3;
$final2 = number_format($final);
echo "<br /><b>Total Points:</b> $final2";
Fou-Lu 01-08-2010, 08:19 PM Yes, just use the function as I've used it above, and its called like so:
function reverse_number_format(&$num)
{
$num = (float)str_replace(',', '', $num);
}
// Call:
// print $credits3; // Say that says 13,445,908
reverse_number_format($credits3);
// print $credits3; // Now this says 13445908
Now I'm not 100% certain if I followed you're criteria correctly. I was under the impression you wanted to alter the original variable, is that correct?
ninnypants 01-08-2010, 08:57 PM The use of a function is to do something that you use commonly so tho you don't have to write out the process, so you would use Fou-Lu's function
// the & causes the variable to be passed as a reference
//to the global variabl instead of just passing the value through
function reverse_number_format(&$num)
{
$num = (float)str_replace(',', '', $num);
}
And your code would become
function reverse_number_format(&$num)
{
$num = (float)str_replace(',', '', $num);
}
if (isset($matches[0]) && is_array($matches[0]))
{
list($credits, $nightcredits, $taxcredits, $exptax, $pendexp, $pendcred, $ore, $fish, $clearing, $herbs, $weapon, $other) = $matches[0];
}
echo "<br /><br />Credits: $credits<br />Nightly Credits: $nightcredits<br />Taxed Credits: $taxcredits<br />Exp Tax: $exptax<br />Pending Exp: $pendexp<br />Pending Credits: $pendcred<br />Ore: $ore<br />Fresh Fish: $fish<br />Clearing: $clearing<br />Herbs: $herbs<br />Weapon/Armor: $weapon<br />Other Items: $other";
$credits2 = $credits;
$credits3 = $credits2 * 2;
$taxcred2 = $taxcredits;
$taxcred3 = $taxcred2 * 2;
$exptax2 = $exptax;
$exptax3 = $exptax2 * 2;
reverse_number_format($credits3);
reverse_number_format($taxcred3);
reverse_number_format($exptax3);
$final = $credits3 + $taxcred3 + $exptax3;
$final2 = number_format($final);
echo "<br /><b>Total Points:</b> $final2";
masterofollies 01-08-2010, 10:13 PM I fail to see why it doesn't work now. I get the same thing.
<h2>Rank Calculator</h2>
<?php
if ($_POST['submit']) {
extract($_POST);
$points1 = 0;
$points2 = 100000;
$points3 = 1000000;
$name1 = 'Landlubber';
$name2 = 'Cabin Boy';
$name3 = 'Crow’s Nest';
$matches = array();
$string = "$checkit";
preg_match_all('/\d+(?:,\d{3})*/', $string, $matches);
if (isset($matches[0]) && is_array($matches[0])) // Indicates that matches have occured
{
foreach ($matches[0] AS $matched)
{
echo '<br />Match: ' . $matched;
}
}
if (isset($matches[0]) && is_array($matches[0]))
{
list($credits, $nightcredits, $taxcredits, $exptax, $pendexp, $pendcred, $ore, $fish, $clearing, $herbs, $weapon, $other) = $matches[0];
}
echo "<br /><br />Credits: $credits<br />Nightly Credits: $nightcredits<br />Taxed Credits: $taxcredits<br />Exp Tax: $exptax<br />Pending Exp: $pendexp<br />Pending Credits: $pendcred<br />Ore: $ore<br />Fresh Fish: $fish<br />Clearing: $clearing<br />Herbs: $herbs<br />Weapon/Armor: $weapon<br />Other Items: $other";
$credits2 = $credits;
$credits3 = $credits2 * 2;
$taxcred2 = $taxcredits;
$taxcred3 = $taxcred2 * 2;
$exptax2 = $exptax;
$exptax3 = $exptax2 * 2;
function reverse_number_format(&$num)
{
$num = (float)str_replace(',', '', $num);
}
reverse_number_format($credits2);
reverse_number_format($taxcred2);
reverse_number_format($exptax2);
$final = $credits3 + $taxcred3 + $exptax3;
$final2 = number_format($final);
echo "<br /><b>Total Points:</b> $final2";
}
else {
?>
<form action="rankcalc.php" method="post">
<textarea name="checkit" cols="40" rows="15">
</textarea><br /><br />
<input type="submit" name="submit" value="Calculate">
</form>
<?php
}
?>
I put in the following
Credits 3,381,000
Nightly Credits 10,000
Credits Taxed 0
EXP Taxed 70,462,618
Pending EXP 0
Pending Credits 0
Ore 42
Fresh Fish 263
Clearing Items 0
Healing Herbs 1,390,100
Weapons/Armor 0
Other Items 5
It gives me
Credits: 3,381,000
Nightly Credits: 10,000
Taxed Credits: 0
Exp Tax: 70,462,618
Pending Exp: 0
Pending Credits: 0
Ore: 42
Fresh Fish: 263
Clearing: 0
Herbs: 1,390,100
Weapon/Armor: 0
Other Items: 5
Total Points: 146
The amount for total points it should be showing is this.
73843618
Which is credits and exp tax put together. It would include taxed credits if I had any.
Fou-Lu 01-08-2010, 10:28 PM You're either summing the incorrect numbers, or you're unformatting the wrong numbers. These: $credits3 + $taxcred3 + $exptax3 are all strings, you want to either run reverse_number_format against the $credits3, $taxcred3 and $exptax3, or use $credits2, $taxcred2 and $exptax2 in you're summation.
Looks to me that you're unformat should be changed to $credits3, $taxcred3 and $exptax3 from the 2's.
masterofollies 01-09-2010, 12:10 AM This is what I have now, and it's still 146.
echo "<br /><br />Credits: $credits<br />Nightly Credits: $nightcredits<br />Taxed Credits: $taxcredits<br />Exp Tax: $exptax<br />Pending Exp: $pendexp<br />Pending Credits: $pendcred<br />Ore: $ore<br />Fresh Fish: $fish<br />Clearing: $clearing<br />Herbs: $herbs<br />Weapon/Armor: $weapon<br />Other Items: $other";
$credits2 = $credits *2;
$taxcred2 = $taxcredits *2;
$exptax2 = $exptax *2;
function reverse_number_format(&$num)
{
$num = (float)str_replace(',', '', $num);
}
reverse_number_format($credits2);
reverse_number_format($taxcred2);
reverse_number_format($exptax2);
$final = $credits2 + $taxcred2 + $exptax2;
$final2 = number_format($final);
echo "<br /><b>Total Points:</b> $final2";
JAY6390 01-09-2010, 01:58 AM You can't multiply the numbers BEFORE you use the number format. You also need to assign the values from the function back to variables
$exptax2 = reverse_number_format($exptax2); for example
masterofollies 01-09-2010, 03:47 AM Now it's working. Thank you
|
|