number_format() question EASY

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rynTAU

    number_format() question EASY

    This is an easy question, I'm sure but I can't seem to figure it
    out.

    I'm trying to use the number_format() function to remove all decimal
    places from a number.


    $num = number_format($ num, 0, '.', '');

    So if I have 23.34567 I want 23, and 45.8789 will return with 45.
    The problem is that the this function rounds the number off. so that
    45.8789 returns 46 instead of 45.

    Is there another function? What should I be using.

    Thanks for your help.

  • Jerry Stuckle

    #2
    Re: number_format() question EASY

    rynTAU wrote:
    This is an easy question, I'm sure but I can't seem to figure it
    out.
    >
    I'm trying to use the number_format() function to remove all decimal
    places from a number.
    >
    >
    $num = number_format($ num, 0, '.', '');
    >
    So if I have 23.34567 I want 23, and 45.8789 will return with 45.
    The problem is that the this function rounds the number off. so that
    45.8789 returns 46 instead of 45.
    >
    Is there another function? What should I be using.
    >
    Thanks for your help.
    >
    $num = int($num);

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • ED

      #3
      Re: number_format() question EASY


      "rynTAU" <rtaube@gmail.c omwrote in message
      news:1172083571 .684502.15110@h 3g2000cwc.googl egroups.com...
      This is an easy question, I'm sure but I can't seem to figure it
      out.
      >
      I'm trying to use the number_format() function to remove all decimal
      places from a number.
      >
      >
      $num = number_format($ num, 0, '.', '');
      >
      So if I have 23.34567 I want 23, and 45.8789 will return with 45.
      The problem is that the this function rounds the number off. so that
      45.8789 returns 46 instead of 45.
      >
      Is there another function? What should I be using.
      >
      Thanks for your help.
      >
      You just need to cast to an integer, ie:

      $num= 23.34567;
      $num = (int) $num;

      $num then equals 23,
      cheers,
      ED


      Comment

      • rynTAU

        #4
        Re: number_format() question EASY

        Thanks, I knew it was something so easy that I couldn't find anything
        about it online. :)



        On Feb 21, 10:53 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        rynTAU wrote:
        This is an easy question, I'm sure but I can't seem to figure it
        out.
        >
        I'm trying to use the number_format() function to remove all decimal
        places from a number.
        >
        $num = number_format($ num, 0, '.', '');
        >
        So if I have 23.34567 I want 23, and 45.8789 will return with 45.
        The problem is that the this function rounds the number off. so that
        45.8789 returns 46 instead of 45.
        >
        Is there another function? What should I be using.
        >
        Thanks for your help.
        >
        $num = int($num);
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstuck...@attgl obal.net
        =============== ===

        Comment

        • Anonymous

          #5
          Re: number_format() question EASY

          Jerry Stuckle wrote:
          >
          rynTAU wrote:
          This is an easy question, I'm sure but I can't seem to figure it
          out.

          I'm trying to use the number_format() function to remove all decimal
          places from a number.


          $num = number_format($ num, 0, '.', '');

          So if I have 23.34567 I want 23, and 45.8789 will return with 45.
          The problem is that the this function rounds the number off. so that
          45.8789 returns 46 instead of 45.

          Is there another function? What should I be using.

          Thanks for your help.
          >
          $num = int($num);
          I would advise against type casting. It can produce strage results under
          some circumstances.

          He would be better of using $num = floor($num), that's what the function
          is here for.

          ceil() always rounds up, floor() always rounds down, round() rounds
          according to mathematical rules (down on .0 to .4, up on .5 to .9

          Bye!

          Comment

          • Jerry Stuckle

            #6
            Re: number_format() question EASY

            Anonymous wrote:
            Jerry Stuckle wrote:
            >rynTAU wrote:
            >>This is an easy question, I'm sure but I can't seem to figure it
            >>out.
            >>>
            >>I'm trying to use the number_format() function to remove all decimal
            >>places from a number.
            >>>
            >>>
            >>$num = number_format($ num, 0, '.', '');
            >>>
            >>So if I have 23.34567 I want 23, and 45.8789 will return with 45.
            >>The problem is that the this function rounds the number off. so that
            >>45.8789 returns 46 instead of 45.
            >>>
            >>Is there another function? What should I be using.
            >>>
            >>Thanks for your help.
            >>>
            >$num = int($num);
            >
            I would advise against type casting. It can produce strage results under
            some circumstances.
            >
            He would be better of using $num = floor($num), that's what the function
            is here for.
            >
            ceil() always rounds up, floor() always rounds down, round() rounds
            according to mathematical rules (down on .0 to .4, up on .5 to .9
            >
            Bye!
            And int() always truncates and returns an integer - which is exactly
            what he wants. See the doc.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Curtis

              #7
              Re: number_format() question EASY

              Jerry Stuckle wrote:
              rynTAU wrote:
              >This is an easy question, I'm sure but I can't seem to figure it
              >out.
              >>
              >I'm trying to use the number_format() function to remove all decimal
              >places from a number.
              >>
              >>
              >$num = number_format($ num, 0, '.', '');
              >>
              >So if I have 23.34567 I want 23, and 45.8789 will return with 45.
              >The problem is that the this function rounds the number off. so that
              >45.8789 returns 46 instead of 45.
              >>
              >Is there another function? What should I be using.
              >>
              >Thanks for your help.
              >>
              >
              $num = int($num);
              >
              Actually, Jerry, your code would produce an E_USER_FATAL, since there
              is no built-in function called int(). Any of these will work:

              $n = 25.2829;
              $n = (int) $n;
              $n = intval($n);
              $n = floor($n);

              --
              Curtis, http://dyersweb.com

              Comment

              • Johannes Vogel

                #8
                Re: number_format() question EASY

                Hi

                Anonymous wrote:
                Jerry Stuckle wrote:
                >$num = int($num);
                I would advise against type casting. It can produce strage results under
                some circumstances.
                He would be better of using $num = floor($num), that's what the function
                is here for.
                ceil() always rounds up, floor() always rounds down, round() rounds
                according to mathematical rules (down on .0 to .4, up on .5 to .9
                What's about minus values?
                We don't know what the OP would like to get.

                floor(-2.8) =-3
                (int)-2.8 =-2

                HTH, Johannes

                Comment

                • Jerry Stuckle

                  #9
                  Re: number_format() question EASY

                  Johannes Vogel wrote:
                  Hi
                  >
                  Anonymous wrote:
                  >Jerry Stuckle wrote:
                  >>$num = int($num);
                  >I would advise against type casting. It can produce strage results under
                  >some circumstances.
                  >He would be better of using $num = floor($num), that's what the function
                  >is here for.
                  >ceil() always rounds up, floor() always rounds down, round() rounds
                  >according to mathematical rules (down on .0 to .4, up on .5 to .9
                  >
                  What's about minus values?
                  We don't know what the OP would like to get.
                  >
                  floor(-2.8) =-3
                  (int)-2.8 =-2
                  >
                  HTH, Johannes
                  int(-2.8) is -2, as you indicated. And the operator indicated he wanted
                  to truncate the value, so floor() would in this case provide an
                  incorrect value.

                  Now if he said he wanted the next lower integer, your solution would be
                  correct.


                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: number_format() question EASY

                    Curtis wrote:
                    Jerry Stuckle wrote:
                    >rynTAU wrote:
                    >>This is an easy question, I'm sure but I can't seem to figure it
                    >>out.
                    >>>
                    >>I'm trying to use the number_format() function to remove all decimal
                    >>places from a number.
                    >>>
                    >>>
                    >>$num = number_format($ num, 0, '.', '');
                    >>>
                    >>So if I have 23.34567 I want 23, and 45.8789 will return with 45.
                    >>The problem is that the this function rounds the number off. so that
                    >>45.8789 returns 46 instead of 45.
                    >>>
                    >>Is there another function? What should I be using.
                    >>>
                    >>Thanks for your help.
                    >>>
                    >>
                    >$num = int($num);
                    >>
                    >
                    Actually, Jerry, your code would produce an E_USER_FATAL, since there is
                    no built-in function called int(). Any of these will work:
                    >
                    $n = 25.2829;
                    $n = (int) $n;
                    $n = intval($n);
                    $n = floor($n);
                    >
                    --
                    Curtis, http://dyersweb.com
                    <sheepish grin>

                    Been doing too much C++ lately, I guess :-)

                    Thanks for the correction.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    Working...