number_format()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    number_format()

    I have realized something which is going to be a headache in the rest of my current project. I don't know how I have not come across this before but let me show you an example:
    Code:
    $x = number_format( 10000 );
    $y = 3;
    echo( $x*$y );
    You would expect 30,000 right? Because the first number format makes that number 10,000 it is now a string (I think) and when operated upon, it converts it to a number as if it were 10.000 (ie just 10).

    How can I get around this? Am I going to have to double up on all my variables and only number_format them after they are 100% done with?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the string "10,000" is cut before the comma while conversion back to number.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Yeah, well in Australia we're the opposite. How can I change that?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        sorry, but the locale isn't used in number_format() .... (but in some other *_format() functions). corrected that (read again).

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          number_format() returns a string. So, best way to go about this, is to have your integer in it's own variable $x and then have it's formatted version in another variable $x_formatted.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            OK, so is there a way to change it so that it recognises 10,000 as 10000? Changing something in the php.ini file? I have been trying to search about changing the locale but nothing yet about php.ini?

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by Markus
              number_format() returns a string. So, best way to go about this, is to have your integer in it's own variable $x and then have it's formatted version in another variable $x_formatted.
              Bugger, I really didn't want to do it that way. Thanks for your help.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by TheServant
                OK, so is there a way to change it so that it recognises 10,000 as 10000?
                remove any non-digits and non-commas from the string and the string-number conversion should run smoothly.

                or use Markus' way
                Last edited by Dormilich; Mar 11 '09, 01:29 PM. Reason: and another possibility

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  Unfortunately what I will have to do is format them as they are echoed, which is just cramping my php/html separation :(

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    why that (don't understand)?

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      Well instead of making another (formatted) variable for each variable I need to format (most of them), it's easier to just format them as they come out.
                      Code:
                      $x = 10000;
                      $y = 3;
                      $z = $x * $y;
                      echo( number_format($x) . " " . number_format($z) );
                      Compared to:

                      Code:
                      $x = 10000;
                      $x_formatted = number_format($x);
                      $y = 3;
                      $z = $x * $y;
                      $z_formatted = number_format($z);
                      echo( $x . " " . $z );

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        some speed-up:
                        Code:
                        echo number_format($x), " ", number_format($z);

                        Comment

                        • TheServant
                          Recognized Expert Top Contributor
                          • Feb 2008
                          • 1168

                          #13
                          Really? I have seen that before but I have always joined strings with "."? That's definitely faster?

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            the point there is: with the commas, you dont need to do the string concatenation thus saving time an output (the Zero bytes). see PHP Performance

                            Comment

                            • TheServant
                              Recognized Expert Top Contributor
                              • Feb 2008
                              • 1168

                              #15
                              Makes sense. Thanks for the tip (and the link)... Really gtg bed. Thanks for the chat guys.

                              Comment

                              Working...