Math.floor

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

    Math.floor

    I wanna round an value in a javascript Down to nearest hundred.
    For example: 3234 = 3200 and 3890 = 3800
    Always down and to hundred.
    I have tried Math.floor but that just round down to ten.
    Is there any way 2 do this??

    Regards
    /Henke
  • Mick White

    #2
    Re: Math.floor

    Henke wrote:
    [color=blue]
    > I wanna round an value in a javascript Down to nearest hundred.
    > For example: 3234 = 3200 and 3890 = 3800
    > Always down and to hundred.
    > I have tried Math.floor but that just round down to ten.
    > Is there any way 2 do this??
    >[/color]

    Math.floor(numb er/100)*100

    Mick

    Comment

    • McKirahan

      #3
      Re: Math.floor

      "Henke" <hlo@thomsonfak ta.se> wrote in message
      news:b3f1830e.0 501130551.4a0c2 966@posting.goo gle.com...[color=blue]
      > I wanna round an value in a javascript Down to nearest hundred.
      > For example: 3234 = 3200 and 3890 = 3800
      > Always down and to hundred.
      > I have tried Math.floor but that just round down to ten.
      > Is there any way 2 do this??
      >
      > Regards
      > /Henke[/color]

      alert(parseInt( 3234/100,10));
      alert(parseInt( 3890/100,10));


      Comment

      • Lee

        #4
        Re: Math.floor

        Henke said:[color=blue]
        >
        >I wanna round an value in a javascript Down to nearest hundred.
        >For example: 3234 = 3200 and 3890 = 3800
        >Always down and to hundred.
        >I have tried Math.floor but that just round down to ten.
        >Is there any way 2 do this??[/color]

        Several, including n-n%100;

        Comment

        • Dr John Stockton

          #5
          Re: Math.floor

          JRS: In article <q_6dnbZ_hPYiHn vcRVn-jg@comcast.com> , dated Thu, 13 Jan
          2005 08:22:05, seen in news:comp.lang. javascript, McKirahan
          <News@McKirahan .com> posted :[color=blue]
          >"Henke" <hlo@thomsonfak ta.se> wrote in message
          >news:b3f1830e. 0501130551.4a0c 2966@posting.go ogle.com...[color=green]
          >> I wanna round an value in a javascript Down to nearest hundred.
          >> For example: 3234 = 3200 and 3890 = 3800[/color][/color]
          [color=blue]
          >alert(parseInt (3234/100,10));
          >alert(parseInt (3890/100,10));[/color]

          That involves an implicit conversion to String and an explicit
          conversion back to Number; it should be slower than a pure mathematical
          method. Also, it gives an incorrect answer.

          If the OP happens to want a string result,
          x = String(N/100).replace(/\..*./, '')+'00'
          could be speed-tested; but the result may be unsatisfactory for N<100.

          If negative numbers are possible, should -777 go to -700 or -800 ?

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...