Rounding in MS Access

后端 未结 12 662
夕颜
夕颜 2020-11-28 14:23

Whats the best way to round in VBA Access?

My current method utilizes the Excel method

Excel.WorksheetFunction.Round(...

But I am l

相关标签:
12条回答
  • 2020-11-28 14:54

    If you're talking about rounding to an integer value (and not rounding to n decimal places), there's always the old school way:

    return int(var + 0.5)
    

    (You can make this work for n decimal places too, but it starts to get a bit messy)

    0 讨论(0)
  • 2020-11-28 14:58
    VBA.Round(1.23342, 2) // will return 1.23
    
    0 讨论(0)
  • 2020-11-28 14:59
    1 place = INT(number x 10 + .5)/10
    3 places = INT(number x 1000 + .5)/1000
    

    and so on.You'll often find that apparently kludgy solutions like this are much faster than using Excel functions, because VBA seems to operate in a different memory space.

    eg If A > B Then MaxAB = A Else MaxAB = B is about 40 x faster than using ExcelWorksheetFunction.Max

    0 讨论(0)
  • 2020-11-28 15:04

    In Switzerland and in particulat in the insurance industry, we have to use several rounding rules, depending if it chash out, a benefit etc.

    I currently use the function

    Function roundit(value As Double, precision As Double) As Double
        roundit = Int(value / precision + 0.5) * precision
    End Function
    

    which seems to work fine

    0 讨论(0)
  • 2020-11-28 15:07

    Int and Fix are both useful rounding functions, which give you the integer part of a number.

    Int always rounds down - Int(3.5) = 3, Int(-3.5) = -4

    Fix always rounds towards zero - Fix(3.5) = 3, Fix(-3.5) = -3

    There's also the coercion functions, in particular CInt and CLng, which try to coerce a number to an integer type or a long type (integers are between -32,768 and 32,767, longs are between-2,147,483,648 and 2,147,483,647). These will both round towards the nearest whole number, rounding away from zero from .5 - CInt(3.5) = 4, Cint(3.49) = 3, CInt(-3.5) = -4, etc.

    0 讨论(0)
  • 2020-11-28 15:08

    Be careful, the VBA Round function uses Banker's rounding, where it rounds .5 to an even number, like so:

    Round (12.55, 1) would return 12.6 (rounds up) 
    Round (12.65, 1) would return 12.6 (rounds down) 
    Round (12.75, 1) would return 12.8 (rounds up)   
    

    Whereas the Excel Worksheet Function Round, always rounds .5 up.

    I've done some tests and it looks like .5 up rounding (symmetric rounding) is also used by cell formatting, and also for Column Width rounding (when using the General Number format). The 'Precision as displayed' flag doesn't appear to do any rounding itself, it just uses the rounded result of the cell format.

    I tried to implement the SymArith function from Microsoft in VBA for my rounding, but found that Fix has an error when you try to give it a number like 58.55; the function giving a result of 58.5 instead of 58.6. I then finally discovered that you can use the Excel Worksheet Round function, like so:

    Application.Round(58.55, 1)

    This will allow you to do normal rounding in VBA, though it may not be as quick as some custom function. I realize that this has come full circle from the question, but wanted to include it for completeness.

    0 讨论(0)
提交回复
热议问题