none
Why System.Math.Round(0.25,1) = .2?

    Question

  • Dear guys,

        Can anyone explain why the result of System.Math.Round(0.25,1) = 2 in vb.net? I tried in sql server and excel but the result is 3?


    thankyou for the help!.


    thanks and regards,
    Jeff
    Jeff®
    Friday, December 12, 2008 9:56 AM

Answers

All replies

  • System.Math.Round takes an optional third parameter of type System.MidpointRounding
    I believe, and your result indicates, that the default behaviour of the method is System.MidpointRounding.ToEven, which rounds to the nearest even value from midpoints. If you want to round the number up in midpoint cases, just specify AwayFromZero.
    • Proposed as answer by John Anthony Oliver Monday, December 15, 2008 5:08 AM
    • Marked as answer by jeffrey_q Tuesday, December 16, 2008 1:49 AM
    Friday, December 12, 2008 10:10 AM
  • It's explained well in the help for Math.Round.  If you want a different result, use an overload that lets you specify the rounding.
    Friday, December 12, 2008 10:11 AM
  •  Thanks for the explanation how about in visual basic 6? Round(0.25,1) = 2? is the default method is the same in .net?

    thanks in advance,

    Jeff®
    Monday, December 15, 2008 12:44 AM
  •  This is a vb8 forum, not vb6.
    Monday, December 15, 2008 3:15 AM
  • jeffrey_q said:

     Thanks for the explanation how about in visual basic 6? Round(0.25,1) = 2? is the default method is the same in .net?

    thanks in advance,


    Jeff®



    Hi,

    Yes if you get the same result.

    In Vb.Net try.>>

    MessageBox.Show(Math.Round(0.25, 1, MidpointRounding.AwayFromZero).ToString)


    Vb.Net for 2002 and 2003 are both major version 7

    Vb.Net 2005 editions are all major version 8,

    Vb.Net 2008 editions are all major version 9

    Vb.Net 2010 editions will be major version 10 with Framework 4.0

    This forum does not support VB6 or any earlier versions of Visual Basic, it does not support VBA or VbScript either.

    Please see my reply in this thread for a link to a VB6 forum.>>

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/7b214b27-44b3-4eb2-8ca2-4dbdde754f2d



    Renee Culver said:

     This is a vb8 forum, not vb6.

    Hi Renee,

    This forum supports VB7 onwards.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b


    Regards,

    John

    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835

    Get something FREE for Christmas ( offer valid till 31st December, 2008 )
     for your computer.>> CLICK HERE. 
    • Marked as answer by jeffrey_q Tuesday, December 16, 2008 1:46 AM
    • Edited by John Anthony Oliver Tuesday, December 23, 2008 12:20 AM ................................................................
    Monday, December 15, 2008 3:44 AM
  • Thanks John for the help and reply. I'am sorry for asking vb 6 questions I just testing in both versions.

    thanks and regards,

    Jeff®
    Tuesday, December 16, 2008 1:48 AM
  • jeffrey_q said:

    Thanks John for the help and reply. I'am sorry for asking vb 6 questions I just testing in both versions.

    thanks and regards,


    Jeff®



    Hi JJeff®,

    You are welcome. :-)

    By the way, I was just letting you know this is a Vb.Net only forum, I am not a forum moderator, in case you thought I was, although lots of forum members have thought that I am a moderator.

    Have a good Christmas and New Year. :-)


    Regards,

    John
    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    Tuesday, December 16, 2008 3:27 AM
  • Thanks John for the help. Merry christmas and Happy new year.! cheers :-)
    Jeff®
    Monday, December 22, 2008 10:02 AM