LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 48
Default Rounding in VBA - Any ideas?

Hi all,

This rounding issue in VBA drive me crazy!
Many posts report that VBA uses the bankers rounding. But I need the standard
rounding.

So I tried to make my own Mathematical Rounding function. For some
mysterieous reasons it does not always work as expected.

Anyone who likes solving strange behaviour of VBA is kindly invited to see
why.

MathRound(101.68000000000001,5) works OK -- 101.68000
MathRound(102.63000000000001,5) does not work --- 102.63001 (102.63000
expected)

The function:
Function MathRound(dblInput As Double, intDigits As Integer) As Double
'Rounds the input value (DblInput) to the number of digits specified in
intDigits
'The container values dblContainer and dblContner seem to be necessary
because
'without them the calculations are wrongly passed through.
Dim dblContainer As Double
Dim lngExpon As Long
Dim lngLong As Long
Dim dblContner As Double
dblContainer = dblInput + 5 * (10 ^ -(intDigits + 1)) 'Add a bit to make
sure truncation is done right.
lngExpon = 10 ^ intDigits 'calculate the power of ten to be multiplied
based on the number of digits
dblContner = Round(dblContainer * lngExpon, 0) 'PUT value in second
container
lngLong = Fix(dblContner) 'truncate value
MathRound = lngLong / lngExpon 'Devide back to the correct number of
digits
End Function

Thanks!

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...tions/200809/1

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chart Ideas??? Judi Excel Discussion (Misc queries) 2 August 30th 07 01:02 PM
Help with a Task...any ideas? Dave Excel Discussion (Misc queries) 1 August 21st 07 06:32 PM
Anyone else have any ideas?? M&M[_2_] Excel Discussion (Misc queries) 3 August 11th 07 01:51 PM
Any Ideas? GAIL HORVATH Excel Worksheet Functions 2 May 30th 05 04:17 PM
Any Ideas Greg B Excel Discussion (Misc queries) 7 May 16th 05 03:41 AM


All times are GMT +1. The time now is 09:35 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"