Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    Opposite of number format

    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.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You'll have to write a custom for that:
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I believe I am doing this wrong.

    PHP Code:
    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"
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yes, just use the function as I've used it above, and its called like so:
    PHP Code:

    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    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
    PHP Code:
    // 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
    PHP Code:
    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"

  • #6
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    I fail to see why it doesn't work now. I get the same thing.

    PHP Code:
    <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
    Code:
    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
    Code:
    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.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    masterofollies (01-09-2010)

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    This is what I have now, and it's still 146.

    PHP Code:
    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"
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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
    PHP Code:
    $exptax2 reverse_number_format($exptax2); 
    for example
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    masterofollies (01-09-2010)

  • #10
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Now it's working. Thank you
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •