Results 1 to 8 of 8

Thread: Round Values

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Round Values

    Hello
    I have an application that need to round some values, with a normal symetric round, but the options available in the round function doesn't allow me to do this kind of round.

    For example, if i have
    vb.net Code:
    1. ?Math.Round(0.275,2)
    2. 0.28
    3. ?Math.Round(1.275,2)
    4. 1.27
    5. ?Math.Round(1.275,2,MidpointRounding.AwayFromZero)
    6. 1.27
    7. ?Math.Round(1.275,2,MidpointRounding.ToEven)
    8. 1.27

    The only way that looks like that i have a symmetric round it's when i use the format function:

    vb.net Code:
    1. ?Format(0.275,"N2")
    2. "0.28"
    3. ?Format(1.275,"N2")
    4. "1.28"

    I read some info available at msdn, i understand the bankers round technique, but when using the midpoint rounding the round should be ok, and it isn't. I'm missing something here?

    Can i use the Format function and by using the Nx string, i have a symmetric rounding always?

    Thanks

    Meanwhile i have this function to do the job:
    vb.net Code:
    1. Public Function MyRound(ByVal valor As Decimal, ByVal casasDecimais As Integer) As Decimal
    2.         Dim sinal As Integer = Math.Sign(valor)
    3.         Dim escala As Decimal = Math.Pow(10, casasDecimais)
    4.         Dim arredondado As Decimal = Math.Floor(Math.Abs(valor) * escala + 0.5D)
    5.         Return ((sinal * arredondado) / escala)
    6.     End Function

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Round Values

    Hi,

    The Math.Round method works on Decimal type values, but you are passing it Doubles in your examples

    vb Code:
    1. ?Math.Round(CDec(0.275), 2)
    2. '0.28
    3. ?Math.Round(CDec(1.275), 2)
    4. '1.28
    5. ?Math.Round(CDec(1.275), 2, MidpointRounding.AwayFromZero)
    6. '1.28
    7. ?Math.Round(CDec(1.275), 2, MidpointRounding.ToEven)
    8. '1.28

    Turning Option Strict On is a good idea. It will show some type conversion errors in your own round function, but unfortunately won't pick up the problem when you pass in literal values.

    EDIT
    Strike that. It of course takes Doubles, so I have no idea why it is rounding like it does in your examples.
    Last edited by Inferrd; Oct 25th, 2011 at 07:08 AM.

  3. #3
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Round Values

    A little more digging reveals that the problem is the precision with which floating point numbers are stored in Double type variables.

    Try:
    MessageBox.Show("1.275 is stored as " & 1.275.ToString("G17"))

    More info here

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,517

    Re: Round Values

    Micky_pt,


    did you find which rounding method will produce the correct answer consistantly? I never knew Math.Round had that problem.

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Round Values

    I always did this:

    Format(2.65, "0.#")

    And for your function why wouldn't you use ....?:
    vb Code:
    1. Public Function MyRound(ByVal valor As Decimal, ByVal casasDecimais As Integer) As Decimal
    2.     retrun Format(valor, "N" & casasDecimais.ToString))
    3. End Function

    Kris

  6. #6

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Round Values

    Thanks for all the answers.

    @Inferrd
    Thanks for looking for the answer for this "issue". In the end the answer it's that when the number isn't a binary fraction you may ou may not have the correct round.
    But anyway the round function with the midpoint rounding should work, even if i use a double, and it doesn't, or I'm missing another "issue" here?!

    @wes4dbt
    Like i wrote, the method that gives me the correct value it's the Format, at least for the tests that i made, but i don't have 100% sure...

    @i00
    And that's why i wrote MyRound.

    So after all the tests and all the reading, i came to the conclusion that i should use always decimal values when perform rounding, just to avoid some surprise in the end.

    Thanks

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  7. #7
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Round Values

    I found it best (somewhat the same way you did) that when dealing with low-precision decimal values, like currency or anything else up to around 5 decimal places max, using the "Decimal" type alleviated most of my headaches. It's precise and exact. Traditional float types like Single and Double work best when you need to grind a lot of numeric data fast, you're not doing anything like rounding, and you don't mind the value might be a little off.

    For most business and industrial apps, I use Decimal exclusively. For things like games, Singles and Doubles work fine.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Round Values

    This article explains this behavior.

    http://weblogs.asp.net/sfurman/archi...3/07/3537.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width