ROUND

ROUND(expression [,numdecimalplaces])

Returns a number rounded to a given number of decimal places (Double).


expressionThe expression to round.
numdecimalplaces(Optional) The number of decimal places to round to (Long) (0).

REMARKS
* This function will either round up or down depending on the last digits.
* This function can be used to round a floating-point or fixed-point decimal to a specified number of places.
* "numdecimalplaces" must be greater than or equal 0.
* If "numdecimalplaces" is left blank, then 0 is used.
* If "numdecimalplaces" is left blank, then an Integer is returned.
* If "numdecimalplaces" < 0, then you will get a run-time error 5.
* This function uses Bankers Rounding when the last digit is a 5 which can be very confusing.
* If you need normal rounding, then you should use the Application.WorksheetFunction.Round function.
* You can use the INT function to return the number rounded down to the nearest integer.
* The equivalent .NET function is System.Math.Round
* link - answers.microsoft.com/en-us/msoffice/forum/all/serious-excelvba-bug/a15721d6-02f4-4eaa-a49e-6d73779e0c51
* For the Microsoft documentation refer to learn.microsoft.com

Debug.Print Round(123.456, 0)           '= 123  
Debug.Print Round(123.456, 1) '= 123.5
Debug.Print Round(123.456, 2) '= 123.46
Debug.Print Round(123.456, 3) '= 123.456

Debug.Print Round(56.78, 0) '= 57
Debug.Print Round(56.78, 1) '= 56.8
Debug.Print Round(56.78, 2) '= 56.78
Debug.Print Round(56.78, 3) '= 56.78

Debug.Print Round(0.5, 0) '= 0
Debug.Print Round(0.50000000001, 0) '= 1
Debug.Print Round(1.5, 0) '= 2
Debug.Print Round(2.5, 0) '= 2
Debug.Print Round(3.5, 0) '= 4
Debug.Print Round(4.5, 0) '= 4
Debug.Print Round(5.5, 0) '= 6

Dim dbValue As Double
dbValue = 0.9
Debug.Print Round(dbValue) '= 1

dbValue = 0.9
Debug.Print Round(dbValue, 1) '= 0.9

dbValue = 0.149
Debug.Print Round(dbValue, 2) '= 0.15

dbValue = 0.169
Debug.Print Round(dbValue, 2) '= 0.17

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top