rounding off numbers

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

    rounding off numbers

    Is there a script that will round off a number to a certain number of
    decimal places? -Allen Thompson


  • Dennis M. Marks

    #2
    Re: rounding off numbers

    In article <LxGCb.99$sW5.5 0@newsread2.new s.atl.earthlink .net>, Allen
    Thompson <genericjoe@min dspring.com> wrote:
    [color=blue]
    > Is there a script that will round off a number to a certain number of
    > decimal places? -Allen Thompson
    >
    >[/color]

    1. Let x = the number of decimal places you want.
    2. Multiply the number by 10 raised to x.
    3. Use Math.round() on the answer.
    4. Divide by the number obtained in 2.

    --
    Dennis M. Marks


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

    Comment

    • bagbourne

      #3
      Re: rounding off numbers

      Dennis M. Marks wrote:[color=blue]
      > In article <LxGCb.99$sW5.5 0@newsread2.new s.atl.earthlink .net>, Allen
      > Thompson <genericjoe@min dspring.com> wrote:
      >
      >[color=green]
      >>Is there a script that will round off a number to a certain number of
      >>decimal places? -Allen Thompson
      >>
      >>[/color]
      >
      >
      > 1. Let x = the number of decimal places you want.
      > 2. Multiply the number by 10 raised to x.
      > 3. Use Math.round() on the answer.
      > 4. Divide by the number obtained in 2.
      >[/color]

      or use the toFixed() method of the Number object:



      Comment

      • HikksNotAtHome

        #4
        Re: rounding off numbers

        In article <brfn22$oqs$1@s parta.btinterne t.com>, bagbourne <noway@noway.co m>
        writes:
        [color=blue]
        >Dennis M. Marks wrote:[/color]

        <--snip-->
        [color=blue]
        >
        >or use the toFixed() method of the Number object:[/color]

        alert((0.007).t oFixed(2)
        //gives 0.00

        Which is dropping it to two decimal places, *not* rounding it.



        --
        Randy

        Comment

        • @SM

          #5
          Re: rounding off numbers

          bagbourne a ecrit :
          [color=blue]
          > or use the toFixed() method of the Number object:
          >
          > http://devedge.netscape.com/library/...r.html#1193137[/color]

          very interesting
          they send me to a page to tell me to download NN7
          beuark ! :-(((

          --
          *************** *************** *************** *************** **
          Stéphane MORIAUX : mailto:stephane OTER-MOImoriaux@wana doo.fr
          Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

          *************** *************** *************** *************** **


          Comment

          • Dennis M. Marks

            #6
            Re: rounding off numbers

            In article <20031213155410 .11891.00002906 @mb-m11.aol.com>,
            HikksNotAtHome <hikksnotathome @aol.com> wrote:
            [color=blue]
            > In article <brfn22$oqs$1@s parta.btinterne t.com>, bagbourne <noway@noway.co m>
            > writes:
            >[color=green]
            > >Dennis M. Marks wrote:[/color]
            >
            > <--snip-->
            >[color=green]
            > >
            > >or use the toFixed() method of the Number object:[/color]
            >
            > alert((0.007).t oFixed(2)
            > //gives 0.00
            >
            > Which is dropping it to two decimal places, *not* rounding it.[/color]


            What kind of misquoting are you doing? My response was about
            Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:
            1. Let x = the number of decimal places you want.
            2. Multiply the number by 10 raised to x.
            3. Use Math.round() on the answer.
            4. Divide by the number obtained in 2.

            --
            Dennis M. Marks


            -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
            http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
            -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

            Comment

            • @SM

              #7
              Re: rounding off numbers

              "Dennis M. Marks" a ecrit :
              [color=blue]
              > My response was about
              > Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:
              > 1. Let x = the number of decimal places you want.
              > 2. Multiply the number by 10 raised to x.
              > 3. Use Math.round() on the answer.
              > 4. Divide by the number obtained in 2.[/color]

              and ???

              x = Math.round(2*10 00)/1000;
              gives
              x = 2
              where are the missing .000 to obtain 2.000

              --
              *************** *************** *************** *************** **
              Stéphane MORIAUX : mailto:stephane OTER-MOImoriaux@wana doo.fr
              Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

              *************** *************** *************** *************** **


              Comment

              • Richard Cornford

                #8
                Re: rounding off numbers

                "Dennis M. Marks" <denmarks@dcsi. net> wrote in message
                news:1312200315 06178104%denmar ks@dcsi.net...[color=blue]
                >In article <20031213155410 .11891.00002906 @mb-m11.aol.com>,
                >HikksNotAtHo me <hikksnotathome @aol.com> wrote:
                >[color=green]
                >>In article <brfn22$oqs$1@s parta.btinterne t.com>,
                >>bagbourne <noway@noway.co m> writes:
                >>[color=darkred]
                >>>Dennis M. Marks wrote:[/color]
                >>
                >><--snip-->[color=darkred]
                >>>
                >>>or use the toFixed() method of the Number object:[/color]
                >>
                >>alert((0.007) .toFixed(2)
                >>//gives 0.00
                >>
                >>Which is dropping it to two decimal places, *not*
                >>rounding it.[/color]
                >
                >What kind of misquoting are you doing? ...[/color]
                <snip>

                There is no misquoting, Randy is responding to bagbourne's reply to your
                post and has quoted the parts of that post that he is responding to
                verbatim.

                Richard.


                Comment

                • Dennis M. Marks

                  #9
                  Re: rounding off numbers

                  In article <3FDBA9A9.E426D 870@wanadoo.fr> , <"@SM"> wrote:
                  [color=blue]
                  > "Dennis M. Marks" a ecrit :
                  >[color=green]
                  > > My response was about
                  > > Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:
                  > > 1. Let x = the number of decimal places you want.
                  > > 2. Multiply the number by 10 raised to x.
                  > > 3. Use Math.round() on the answer.
                  > > 4. Divide by the number obtained in 2.[/color]
                  >
                  > and ???
                  >
                  > x = Math.round(2*10 00)/1000;
                  > gives
                  > x = 2
                  > where are the missing .000 to obtain 2.000
                  >[/color]
                  <snip>

                  The original question was:
                  Is there a script that will round off a number to a certain number of
                  decimal places? -Allen Thompson

                  It did not state "display" a certain number of decimal places. My
                  answer is mathematically correct. To show trailing zeros after the
                  decimal point text zeros would have to be affixed to the end. It's too
                  bad that javascript doesn't have output formatting like some other
                  languages.

                  --
                  Dennis M. Marks


                  -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                  http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                  -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                  Comment

                  • Dr John Stockton

                    #10
                    Re: rounding off numbers

                    JRS: In article <13122003082539 8921%denmarks@d csi.net>, seen in
                    news:comp.lang. javascript, Dennis M. Marks <denmarks@dcsi. net> posted at
                    Sat, 13 Dec 2003 08:25:39 :-[color=blue]
                    >In article <LxGCb.99$sW5.5 0@newsread2.new s.atl.earthlink .net>, Allen
                    >Thompson <genericjoe@min dspring.com> wrote:
                    >[color=green]
                    >> Is there a script that will round off a number to a certain number of
                    >> decimal places?[/color][/color]

                    That is not, as written, a meaningful question. All javascript numbers
                    are 64-bit IEEE Doubles, floating-point binary. Only strings can have
                    decimal places. Presumably you want to start with a variable (or
                    expression) of type Number, and generate a String representing that
                    number in decimal with two digits after the decimal point, probably for
                    display.

                    You should have read the FAQ before posting; all of it, but item 4.6
                    refers.

                    [color=blue]
                    >1. Let x = the number of decimal places you want.
                    >2. Multiply the number by 10 raised to x.
                    >3. Use Math.round() on the answer.
                    >4. Divide by the number obtained in 2.[/color]

                    That is the answer to a different question than the one almost certainly
                    intended.

                    You too should have read the FAQ before posting. That gives the nearest
                    (give or take) Number which is a multiple of 10^-x. When displayed,
                    that result will be shown with no more than x digits after the point;
                    but there may be fewer. One might get # or #.# or #.## if x=2.



                    The version in the FAQ has
                    if (S.search && S.search(/\D/)!=-1) { return ''+X }
                    If S.search does not exist, but S.search(/\D/) would have succeeded, the
                    return does not occur. But the following code was written on the
                    assumption that the search had, or would have, failed.

                    I now use
                    if (/\D/.test(S)) { return ''+X }
                    on the basis that it should work in Javascript 1.2 and higher, which is
                    adequate.

                    Otherwise, is there a brief method of testing for a non-digit that works
                    in earlier javascript? Better than
                    for (x=0;x<S.length ;x++) if ((t=S.charAt(x) )<'0'||t>'9') return ''+X
                    ?

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> Jsc maths, dates, sources.
                    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.

                    Comment

                    • Michael Winter

                      #11
                      Re: rounding off numbers

                      @SM wrote on 14 Dec 2003 at Sun, 14 Dec 2003 00:07:11 GMT:
                      [color=blue]
                      > x = Math.round(2*10 00)/1000;
                      > gives
                      > x = 2
                      > where are the missing .000 to obtain 2.000[/color]

                      To be fair, the OP mentioned nothing about trailing zeros, just
                      performing rounding. They are not the same thing. The rounding may
                      just be part of a calculation.

                      If the question was: "How do I display a floating-point value to a
                      certain number of decimal places?", then you could argue about the
                      missing zeros.

                      Mike

                      --
                      Michael Winter
                      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

                      Comment

                      • Fabian

                        #12
                        Re: rounding off numbers

                        Dennis M. Marks hu kiteb:
                        [color=blue][color=green]
                        >> Which is dropping it to two decimal places, *not* rounding it.[/color]
                        >
                        >
                        > What kind of misquoting are you doing? My response was about
                        > Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:[/color]


                        Hey Dennis, you need to lay off the coffee for a while. There wasn't any
                        misquoting, he was simply talking at a slight tangent.


                        --
                        --
                        Fabian
                        Visit my website often and for long periods!
                        AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.


                        Comment

                        • Mike

                          #13
                          Re: rounding off numbers

                          Here is an actual code example of the formula defined by Dennis Marks.

                          function number_ClipToTh eHundredth(valu e) {
                          return Math.round(pars eFloat(value) * 100)/100;
                          }

                          Change the 100 to any decimal place you want, 10, 100, 1000 (10^x).

                          Mike


                          "Allen Thompson" <genericjoe@min dspring.com> wrote in message
                          news:LxGCb.99$s W5.50@newsread2 .news.atl.earth link.net...[color=blue]
                          > Is there a script that will round off a number to a certain number of
                          > decimal places? -Allen Thompson
                          >
                          >[/color]


                          Comment

                          • Fabian

                            #14
                            Re: rounding off numbers

                            Mike hu kiteb:
                            [color=blue]
                            > Here is an actual code example of the formula defined by Dennis Marks.
                            >
                            > function number_ClipToTh eHundredth(valu e) {
                            > return Math.round(pars eFloat(value) * 100)/100;
                            > }
                            >
                            > Change the 100 to any decimal place you want, 10, 100, 1000 (10^x).[/color]

                            To avoid floating errors, it is wise to leave the final divisor off
                            until you are ready to display the number. Wherver possible, it is best
                            to use whole numbers in javascript.


                            --
                            --
                            Fabian
                            Visit my website often and for long periods!
                            AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.


                            Comment

                            • Eric Bohlman

                              #15
                              Re: rounding off numbers

                              "Fabian" <lajzar@hotmail .com> wrote in news:brgqee$33v jm$2@ID-
                              174912.news.uni-berlin.de:
                              [color=blue]
                              > To avoid floating errors, it is wise to leave the final divisor off
                              > until you are ready to display the number. Wherver possible, it is best
                              > to use whole numbers in javascript.[/color]

                              Or any language in which arithmetic is done in floating point. For
                              example, when doing financial calculations, the rule is always to convert
                              all the numbers you use into the smallest units you do business with (for
                              ordinary commerce, that would be cents or your country's equivalent) and
                              only convert to larger units (e.g. dollars) at the very end.

                              There's an entire branch of mathematics (numerical analysis) devoted to the
                              study of the consequences of dealing with concrete representations of
                              numbers rather than ideal abstract numbers. When you study numerical
                              analysis you learn, for example, that if you try to subtract one large
                              floating-point number from another larger floating-point number that's
                              nearly equal to it, you cand lose nearly all of your significant digits
                              (which means, among other things, that the textbook formula for single-pass
                              computation of the standard deviation of a set of data often fails when
                              implemented on a computer). And that if you try to compare two floating-
                              point numbers for exact equality, you often get surprising results. Try:

                              for (i=0;i<=10;i+=0 .1) {document.write (i+"<br>");}

                              and you'll find that the last value displayed isn't exactly ten. Floating-
                              point numbers are *approximations * to real numbers. Floating-point
                              addition, for example, isn't strictly associative even though real-number
                              addition is.

                              Comment

                              Working...