round up

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

    round up

    If I have a value that is 53.123499999, how can I round it to 53.12?
  • Peter Foti

    #2
    Re: round up

    "joeandtel" <anonymous@disc ussions.microso ft.com> wrote in message
    news:5EE33B93-3533-4814-B4ED-620FA1E5F6AE@mi crosoft.com...[color=blue]
    > If I have a value that is 53.123499999, how can I round it to 53.12?[/color]

    FormatNumber( 53.123499999, 2 )

    Regards,
    Peter Foti


    Comment

    • Bob Barrows [MVP]

      #3
      Re: round up

      joeandtel wrote:[color=blue]
      > If I have a value that is 53.123499999, how can I round it to 53.12?[/color]

      Umm - that's not rounding "up".

      In vbscript, you can use the round() function.

      Here's a link to the downloadable documentation:


      Bob Barrows
      --
      Microsoft MVP -- ASP/ASP.NET
      Please reply to the newsgroup. The email account listed in my From
      header is my spam trap, so I don't check it very often. You will get a
      quicker response by posting to the newsgroup.


      Comment

      • Peter Foti

        #4
        Re: round up

        "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
        news:Ozxz1On9DH A.3900@tk2msftn gp13.phx.gbl...[color=blue]
        > joeandtel wrote:[color=green]
        > > If I have a value that is 53.123499999, how can I round it to 53.12?[/color]
        >
        > Umm - that's not rounding "up".[/color]


        Bob, he didn't ask to round up... just to round it.

        [color=blue]
        > In vbscript, you can use the round() function.[/color]


        You could, but it wouldn't provide the functionality that he's asking for.
        The round function is foolishly implemented in my opinion. It rounds even
        numbers down, and odd numbers up. Where would it ever be useful to round
        22.5 to 22, and 21.5 to 22?

        The FormatNumber function will at least round in a sensible way (values of
        5+ are rounded up, values of 4- are rounded down). For example:
        FormatNumber( 22.5, 0 ) = 23
        FormatNumber( 21.5, 0 ) = 22
        Round( 22.5, 0 ) = 22
        Round( 21.5, 0 ) = 22

        Regards,
        Peter Foti




        Comment

        • Bob Barrows [MVP]

          #5
          Re: round up

          Peter Foti wrote:[color=blue]
          > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
          > news:Ozxz1On9DH A.3900@tk2msftn gp13.phx.gbl...[color=green]
          >> joeandtel wrote:[color=darkred]
          >>> If I have a value that is 53.123499999, how can I round it to 53.12?[/color]
          >>
          >> Umm - that's not rounding "up".[/color]
          >
          >
          > Bob, he didn't ask to round up... just to round it.
          >[/color]
          Look at the subject
          --
          Microsoft MVP -- ASP/ASP.NET
          Please reply to the newsgroup. The email account listed in my From
          header is my spam trap, so I don't check it very often. You will get a
          quicker response by posting to the newsgroup.


          Comment

          • Peter Foti

            #6
            Re: round up

            "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
            news:uiD%23gon9 DHA.2696@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > Peter Foti wrote:[color=green]
            > > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
            > > news:Ozxz1On9DH A.3900@tk2msftn gp13.phx.gbl...[color=darkred]
            > >> joeandtel wrote:
            > >>> If I have a value that is 53.123499999, how can I round it to 53.12?
            > >>
            > >> Umm - that's not rounding "up".[/color]
            > >
            > >
            > > Bob, he didn't ask to round up... just to round it.
            > >[/color]
            > Look at the subject[/color]

            Doh! Sorry. :)

            -Peter


            Comment

            • Evertjan.

              #7
              Re: round up

              =?Utf-8?B?am9lYW5kdGV s?= wrote on 18 feb 2004 in
              microsoft.publi c.inetserver.as p.general:
              [color=blue]
              > If I have a value that is 53.123499999, how can I round it to 53.12?[/color]

              The old and safe way:

              n = 53.123499999

              n = int( n * 100 + 0.5 ) / 100

              n = int(n) / 100

              Response.Write FormatNumber(n, 2)

              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • Bob Barrows [MVP]

                #8
                Re: round up

                Peter Foti wrote:[color=blue]
                >
                > You could, but it wouldn't provide the functionality that he's asking
                > for. The round function is foolishly implemented in my opinion. It
                > rounds even numbers down, and odd numbers up. Where would it ever be
                > useful to round
                > 22.5 to 22, and 21.5 to 22?
                >[/color]
                In financial situations. That's why it's called banker's rounding. It's
                intended to avoid the over-enrichment of one party vs. another in financial
                transactions involving rounding by randomizing which way the number in the
                middle will be rounded. Sometimes the bank will get the extra penny,
                sometimes the customer will get it. It would be unfair if one party or the
                other always got the extra penny, wouldn't it?

                Why is your definition the "correct" one? Rounding is defined as setting the
                value of a number to the closest rounded result. By this definition, the
                number ending in 5 cannot be rounded, since neither rounded result is closer
                to 5 than the other. So why do you insist it's correct to always round down
                (or up)? Why not use a method that results in half the numbers being
                rounded down, and half being rounded up? The aggregate result will probably
                be closer to the non-rounded aggregate result than if you chose to always
                round the middle number up or down.

                Bob Barrows
                --
                Microsoft MVP -- ASP/ASP.NET
                Please reply to the newsgroup. The email account listed in my From
                header is my spam trap, so I don't check it very often. You will get a
                quicker response by posting to the newsgroup.


                Comment

                • Peter Foti

                  #9
                  Re: round up

                  "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                  news:epiJztn9DH A.1504@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > Peter Foti wrote:[color=green]
                  > >
                  > > You could, but it wouldn't provide the functionality that he's asking
                  > > for. The round function is foolishly implemented in my opinion. It
                  > > rounds even numbers down, and odd numbers up. Where would it ever be
                  > > useful to round
                  > > 22.5 to 22, and 21.5 to 22?
                  > >[/color]
                  > In financial situations. That's why it's called banker's rounding. It's
                  > intended to avoid the over-enrichment of one party vs. another in[/color]
                  financial[color=blue]
                  > transactions involving rounding by randomizing which way the number in the
                  > middle will be rounded. Sometimes the bank will get the extra penny,
                  > sometimes the customer will get it. It would be unfair if one party or the
                  > other always got the extra penny, wouldn't it?[/color]


                  I've never heard of this before. Though it seems to make sense.

                  [color=blue]
                  > Why is your definition the "correct" one? Rounding is defined as setting[/color]
                  the[color=blue]
                  > value of a number to the closest rounded result. By this definition, the
                  > number ending in 5 cannot be rounded, since neither rounded result is[/color]
                  closer[color=blue]
                  > to 5 than the other. So why do you insist it's correct to always round[/color]
                  down[color=blue]
                  > (or up)? Why not use a method that results in half the numbers being
                  > rounded down, and half being rounded up? The aggregate result will[/color]
                  probably[color=blue]
                  > be closer to the non-rounded aggregate result than if you chose to always
                  > round the middle number up or down.[/color]


                  In school, I was taught that you rounded 1 - 4 down, and 5 - 9 up (the way
                  the FormatNumber does rounding). I don't ever remember being taught
                  banker's rounding. You learn something new every day. :)

                  -Pete


                  Comment

                  • Mark Schupp

                    #10
                    Re: round up

                    > In school, I was taught that you rounded 1 - 4 down, and 5 - 9 up (the way[color=blue]
                    > the FormatNumber does rounding). I don't ever remember being taught
                    > banker's rounding. You learn something new every day. :)[/color]

                    I used to get horribly upset when my children would come home and tell me
                    that they were being taught to round that way (arithmetic rounding). I was
                    taught the other way (banker's rounding). It wasn't until I researched the
                    issue (to prove I was right, of course) that I found out that there were 2
                    different ways. I guess it is logical to teach arithmetic rounding in a math
                    class but there should at least be a mention of the other method.

                    The trick is to be consistent. Another programmer and I once spent 3 days
                    tracking down a 6 cent out of balance condition in a hospitals books (tried
                    to give the accounting trolls the 6 cents but they wouldn't take it). The
                    cause: in one place arithmetic rounding was used while banker's rounding was
                    used elsewhere.

                    --
                    Mark Schupp
                    Head of Development
                    Integrity eLearning



                    "Peter Foti" <peter@Idontwan tnostinkingemai lfromyou.com> wrote in message
                    news:1039l1mbhu poqd1@corp.supe rnews.com...[color=blue]
                    > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                    > news:epiJztn9DH A.1504@TK2MSFTN GP12.phx.gbl...[color=green]
                    > > Peter Foti wrote:[color=darkred]
                    > > >
                    > > > You could, but it wouldn't provide the functionality that he's asking
                    > > > for. The round function is foolishly implemented in my opinion. It
                    > > > rounds even numbers down, and odd numbers up. Where would it ever be
                    > > > useful to round
                    > > > 22.5 to 22, and 21.5 to 22?
                    > > >[/color]
                    > > In financial situations. That's why it's called banker's rounding. It's
                    > > intended to avoid the over-enrichment of one party vs. another in[/color]
                    > financial[color=green]
                    > > transactions involving rounding by randomizing which way the number in[/color][/color]
                    the[color=blue][color=green]
                    > > middle will be rounded. Sometimes the bank will get the extra penny,
                    > > sometimes the customer will get it. It would be unfair if one party or[/color][/color]
                    the[color=blue][color=green]
                    > > other always got the extra penny, wouldn't it?[/color]
                    >
                    >
                    > I've never heard of this before. Though it seems to make sense.
                    >
                    >[color=green]
                    > > Why is your definition the "correct" one? Rounding is defined as setting[/color]
                    > the[color=green]
                    > > value of a number to the closest rounded result. By this definition, the
                    > > number ending in 5 cannot be rounded, since neither rounded result is[/color]
                    > closer[color=green]
                    > > to 5 than the other. So why do you insist it's correct to always round[/color]
                    > down[color=green]
                    > > (or up)? Why not use a method that results in half the numbers being
                    > > rounded down, and half being rounded up? The aggregate result will[/color]
                    > probably[color=green]
                    > > be closer to the non-rounded aggregate result than if you chose to[/color][/color]
                    always[color=blue][color=green]
                    > > round the middle number up or down.[/color]
                    >
                    >
                    > In school, I was taught that you rounded 1 - 4 down, and 5 - 9 up (the way
                    > the FormatNumber does rounding). I don't ever remember being taught
                    > banker's rounding. You learn something new every day. :)
                    >
                    > -Pete
                    >
                    >[/color]


                    Comment

                    Working...