Rounding

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

    Rounding

    How do you round numbers in .NET? I have numbers defined as Single and after
    some calculations I want to round the result to the nearest tenth so I can
    do comparison of results (the rfuzz problem). Does FORMAT round?

    --
    ------------------------------------
    Wayne Wengert
    wayne@wengert.o rg


  • Jon Skeet

    #2
    Re: Rounding

    Wayne Wengert <wayne@wengert. org> wrote:[color=blue]
    > How do you round numbers in .NET? I have numbers defined as Single and after
    > some calculations I want to round the result to the nearest tenth so I can
    > do comparison of results (the rfuzz problem). Does FORMAT round?[/color]

    If you want to compare results, then I suggest that rather than
    rounding you just compare the difference between the expected results
    and the ones you've got. You can specify the number of digits when you
    format, but that'll use (I believe) bankers' rounding, which may or may
    not be what you want.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Phil Price

      #3
      Re: Rounding

      Wayne Wengert wrote:
      [color=blue]
      > How do you round numbers in .NET? I have numbers defined as Single
      > and after some calculations I want to round the result to the nearest
      > tenth so I can do comparison of results (the rfuzz problem). Does
      > FORMAT round?[/color]

      For rounding down:

      System.Math.Flo or(myNumber);
      For rounding up:
      System.Math.Cei ling(myNumber);
      For simple rounding (the kind you do in maths classes at school):
      System.Math.Rou nd(myNumber);
      Obviously these all return int types, and dont effect the parameter
      variable.
      Peace.
      --
      Phil Price
      Student at The University Of Hull
      Got a boner and need fast spank fuel? Our porn sites list sorts tubes, amateurs, teens, MILFs and more-jack straight to the best spots fast - philprice.net


      Comment

      • Steven Licciardi

        #4
        Re: Rounding

        If you multiply the number by 10e10 before you round (using the Math.Round
        above, or just CInt(num), then divide by 10e10, this will give you 10
        decimal places.

        Steven

        "Phil Price" <phil@philprice .net> wrote in message
        news:be9vtr$ih$ 3@newsg4.svr.po l.co.uk...[color=blue]
        > Wayne Wengert wrote:
        >[color=green]
        > > How do you round numbers in .NET? I have numbers defined as Single
        > > and after some calculations I want to round the result to the nearest
        > > tenth so I can do comparison of results (the rfuzz problem). Does
        > > FORMAT round?[/color]
        >
        > For rounding down:
        >
        > System.Math.Flo or(myNumber);
        > For rounding up:
        > System.Math.Cei ling(myNumber);
        > For simple rounding (the kind you do in maths classes at school):
        > System.Math.Rou nd(myNumber);
        > Obviously these all return int types, and dont effect the parameter
        > variable.
        > Peace.
        > --
        > Phil Price
        > Student at The University Of Hull
        > http://www.philprice.net
        > http://www.dcs.hull.ac.uk[/color]


        Comment

        • Jon Skeet

          #5
          Re: Rounding

          Phil Price <phil@philprice .net> wrote:[color=blue]
          > For simple rounding (the kind you do in maths classes at school):
          > System.Math.Rou nd(myNumber);[/color]

          No, that's *not* the kind of rounding most people do in maths classes
          at school. At least, it certainly isn't the kind of rounding *I*
          usually came across. It's bankers' rounding - halves round towards
          even, so Math.Round(1.5) =2, Math.Round(2.5) =2, Math.Round(3.5) =4 etc.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...