Number question

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

    Number question

    Is there a way to "round" (bad use of the word) a number to the nearest 5
    eg
    4 => 5
    7 => 10
    1 => 5
    12 => 15

    Many Thanks for any help provide
    Craig


  • Simon Stienen

    #2
    Re: Number question

    Craig Keightley <dont@spam.me > wrote:[color=blue]
    > Is there a way to "round" (bad use of the word) a number to the nearest 5
    > eg
    > 4 => 5
    > 7 => 10
    > 1 => 5
    > 12 => 15
    >
    > Many Thanks for any help provide
    > Craig[/color]

    $round_x = 5 * ceil($x/5);

    or (*maybe* faster):
    $round_x = $x + ((5 - ($x%5)) % 5);
    --
    Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
    »What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done.«
    -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

    Comment

    • Craig Keightley

      #3
      Re: Number question

      Excellent
      Thanks for that


      "Simon Stienen" <simon.stienen@ news.slashlife. de> wrote in message
      news:zzl46hf7m0 df.dlg@news.dan gerouscat.net.. .[color=blue]
      > Craig Keightley <dont@spam.me > wrote:[color=green]
      >> Is there a way to "round" (bad use of the word) a number to the nearest 5
      >> eg
      >> 4 => 5
      >> 7 => 10
      >> 1 => 5
      >> 12 => 15
      >>
      >> Many Thanks for any help provide
      >> Craig[/color]
      >
      > $round_x = 5 * ceil($x/5);
      >
      > or (*maybe* faster):
      > $round_x = $x + ((5 - ($x%5)) % 5);
      > --
      > Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
      > »What you do in this world is a matter of no consequence,
      > The question is, what can you make people believe that you have done.«
      > -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle[/color]


      Comment

      • J.O. Aho

        #4
        Re: Number question

        Craig Keightley wrote:[color=blue]
        > Is there a way to "round" (bad use of the word) a number to the nearest 5
        > eg
        > 4 => 5
        > 7 => 10
        > 1 => 5
        > 12 => 15[/color]

        function round5($val) {

        $div=floor((int )$val/(int)5);
        if($val%5) {
        $div++;
        }

        return $div * 5;
        }

        Comment

        • Ehtor

          #5
          Re: Number question

          Try this sample:

          for($x=0;$x<30; $x++)
          {
          if( !($x % 5) ) // if mod is zero, we're already there
          {
          $nearest = $x;
          }
          else
          {
          $nearest = (intval($x/5)+1)*5;
          }

          echo "\n $x rounds to $nearest";
          }



          "Craig Keightley" <dont@spam.me > wrote in
          news:41542f09$0 $20250$cc9e4d1f @news-text.dial.pipex .com:
          [color=blue]
          > Is there a way to "round" (bad use of the word) a number to the nearest 5
          > eg
          > 4 => 5
          > 7 => 10
          > 1 => 5
          > 12 => 15
          >
          > Many Thanks for any help provide
          > Craig
          >
          >
          >[/color]

          Comment

          • Ehtor

            #6
            Re: Number question

            Or as a function:

            function another_round5( $x)
            {
            if( !($x % 5) )
            return($x);
            else
            return((intval( $x/5)+1)*5);
            }



            Ehtor <nomail@nomail. com> wrote in
            news:Xns956E750 BD82C5nomailnom ailcom@140.99.9 9.130:
            [color=blue]
            > Try this sample:
            >
            > for($x=0;$x<30; $x++)
            > {
            > if( !($x % 5) ) // if mod is zero, we're already there
            > {
            > $nearest = $x;
            > }
            > else
            > {
            > $nearest = (intval($x/5)+1)*5;
            > }
            >
            > echo "\n $x rounds to $nearest";
            > }
            >
            >
            >
            > "Craig Keightley" <dont@spam.me > wrote in
            > news:41542f09$0 $20250$cc9e4d1f @news-text.dial.pipex .com:
            >[color=green]
            >> Is there a way to "round" (bad use of the word) a number to the
            >> nearest 5 eg
            >> 4 => 5
            >> 7 => 10
            >> 1 => 5
            >> 12 => 15
            >>
            >> Many Thanks for any help provide
            >> Craig
            >>
            >>
            >>[/color]
            >
            >[/color]

            Comment

            • Craig Keightley

              #7
              Re: Number question

              Thanks all but i went with this:
              $round_x = 5 * ceil($x/5);


              "Ehtor" <nomail@nomail. com> wrote in message
              news:Xns956E75D FD247Fnomailnom ailcom@140.99.9 9.130...[color=blue]
              > Or as a function:
              >
              > function another_round5( $x)
              > {
              > if( !($x % 5) )
              > return($x);
              > else
              > return((intval( $x/5)+1)*5);
              > }
              >
              >
              >
              > Ehtor <nomail@nomail. com> wrote in
              > news:Xns956E750 BD82C5nomailnom ailcom@140.99.9 9.130:
              >[color=green]
              >> Try this sample:
              >>
              >> for($x=0;$x<30; $x++)
              >> {
              >> if( !($x % 5) ) // if mod is zero, we're already there
              >> {
              >> $nearest = $x;
              >> }
              >> else
              >> {
              >> $nearest = (intval($x/5)+1)*5;
              >> }
              >>
              >> echo "\n $x rounds to $nearest";
              >> }
              >>
              >>
              >>
              >> "Craig Keightley" <dont@spam.me > wrote in
              >> news:41542f09$0 $20250$cc9e4d1f @news-text.dial.pipex .com:
              >>[color=darkred]
              >>> Is there a way to "round" (bad use of the word) a number to the
              >>> nearest 5 eg
              >>> 4 => 5
              >>> 7 => 10
              >>> 1 => 5
              >>> 12 => 15
              >>>
              >>> Many Thanks for any help provide
              >>> Craig
              >>>
              >>>
              >>>[/color]
              >>
              >>[/color]
              >[/color]


              Comment

              • Hilarion

                #8
                Re: Number question

                > $round_x = 5 * ceil($x/5);

                Your code gives 10 as output for 6 input, where nearest 5
                should be 6.

                Try this:

                $round_x = 5 * round( $x / 5 );

                Hilarion


                Comment

                • Nikolai Chuvakhin

                  #9
                  Re: Number question

                  "Craig Keightley" <dont@spam.me > wrote in message
                  news:<41542f09$ 0$20250$cc9e4d1 f@news-text.dial.pipex .com>...[color=blue]
                  >
                  > Is there a way to "round" (bad use of the word) a number
                  > to the nearest 5 eg
                  > 4 => 5
                  > 7 => 10
                  > 1 => 5
                  > 12 => 15[/color]

                  Sure:

                  $oldnumber = 12;
                  $newnumber = ceil($oldnumber / 5) * 5; // $newnumber = 15...

                  Cheers,
                  NC

                  Comment

                  • Craig Keightley

                    #10
                    Re: Number question

                    Thats what i want it to do:
                    Round Up
                    e.g 6 <= 10
                    therefore 6 == 10

                    "Hilarion" <hilarion@SPAM. op.SMIECI.pl> wrote in message
                    news:cj1h56$c9u $1@news.onet.pl ...[color=blue][color=green]
                    >> $round_x = 5 * ceil($x/5);[/color]
                    >
                    > Your code gives 10 as output for 6 input, where nearest 5
                    > should be 6.
                    >
                    > Try this:
                    >
                    > $round_x = 5 * round( $x / 5 );
                    >
                    > Hilarion
                    >[/color]


                    Comment

                    Working...