CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Rounding a Value

    I was trying to round off a Money Value in a Currency field

    eg,

    Code:
    Dim MV as Currency
    A = 3.00
    B = 1.10
    
    MV = (A * B) + 0.005
    MV = MV / 100
    MV = MV * 100
    Result = 3.31 !

    Currency Variables must therefore have their own rounding rules

    I overcame the problem with

    Code:
    Dim MV as Currency
    A = 3.00
    B = 1.10
    
    MV = (A * B) + 0.005
    MV = (Int(MV * 100)/100)
    Result = 3.30 Correct


    Can anyone suggest a better way to deal with this common calculation requirement

  2. #2
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Rounding a Value

    Check out this link for more information than I've ever seen in one place before about how MS products do rounding.

    http://support.microsoft.com/default...b;en-us;196652

  3. #3
    Join Date
    May 2001
    Posts
    91

    Re: Rounding a Value

    Hi George,

    to round 3.305 to 3.31 is absolutely correct (based on 2 decimals), I just do not understand why you want to get the wrong result??
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

  4. #4
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: Rounding a Value

    I agree... what exactly is the issue here? What was wrong with your results?

    FYI: There are several different ways of rounding off: Round, Int, Floor and Ceil.
    Mike

  5. #5
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Rounding a Value

    Actually, the "correct" result of rounding depends on the application, not on absolute rules. This is especially important in accounting functions, as "rounding bias" can start to introduce error over large amounts of transactions. The link above gives 10 different code examples of rounding appropriate for different business rules.

  6. #6
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441

    Re: Rounding a Value

    Take a look here for a small surprise.

    Hope it helps
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

  7. #7
    Join Date
    May 2001
    Posts
    91

    Re: Rounding a Value

    @deepbuti:
    AFAIK the round function of Microsoft is just buggy (not only in the context you've described in the link you gave), just don't use it.
    It is much better to write your own round function, it's easy.
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

  8. #8
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: Rounding a Value

    It is not buggy. It is a method of rounding known as "bankers rounding". Why I wouldn't have the first clue.

    Believe it or not, it does have its uses. Most noticably it can be implemented to be very quick.

    You can wriet your own function but they are often very slow in comparison, which is usually fine for VB (since, if speed was an issue, you wouldn't use VB).

    Often "accurate" rounding is not AS necessary as one might think. It will often surfice to simply use bankers rounding or return only the integer part. But it does depend on your requirements.
    Mike

  9. #9
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Re: Rounding a Value

    I thought Currency variables worked to 4 decimal places

    Hence in my example I am applying the 0.005 to achieve rounding (up or down) because the currency variable doesn't know I want rounding to 2 decimal places

    The original problem was that I also wanted to truncate any superfluous places in the calc (I only want a 2 decimal result ie, Dollars and Cents)

    What became interesting was that if my result of the calculation was 3.3103
    after I had applied the 0.005 round off factor it became 3.3153, the Currency field then applied rounding again if I tried to chop off the last 2 decimals by dividing the whole value by 100 - this caused an extra rounding to take place.

    As for the IEEE standards - all I can say is YEEEEEEEEEEEEEEEEEEEEE !

    I was hoping to see something like the Math.Round formula out of interest

    (we can always write our own, to be sure)

    Thanks for the comments guys - most interesting !

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

Featured