Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
The following RoundtoUp code is failing in few specific cases as compared with Excel RoundUp:
[TABLE="width: 655"]
<tbody>[TR]
[TD]Number[/TD]
[TD]Digits[/TD]
[TD][/TD]
[TD]RoundtoUp[/TD]
[TD]RoundUp (Excel)[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]1[/TD]
[TD]Ok[/TD]
[TD]'0.1[/TD]
[TD]'0.1[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]2[/TD]
[TD]Ok[/TD]
[TD]'0.01[/TD]
[TD]'0.01[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]3[/TD]
[TD]Ok[/TD]
[TD]'0.01[/TD]
[TD]'0.01[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]4[/TD]
[TD]Error[/TD]
[TD]'0.01[/TD]
[TD]'0.0099[/TD]
[/TR]
[TR]
[TD]-0.0714285714,[/TD]
[TD]2[/TD]
[TD]Ok[/TD]
[TD]'-0.08[/TD]
[TD]'-0.08[/TD]
[/TR]
[TR]
[TD]-1.2345,[/TD]
[TD]3[/TD]
[TD]Ok[/TD]
[TD]'-1.235[/TD]
[TD]'-1.235[/TD]
[/TR]
[TR]
[TD]1.01234012340125,[/TD]
[TD]14[/TD]
[TD]Error[/TD]
[TD]'1.01234012340126[/TD]
[TD]'1.01234012340125[/TD]
[/TR]
</tbody>[/TABLE]
Could not debug the reason for such a behavior. What is a possible fix?
Code:
Public Function RoundtoUp( _
ByVal myNum As Double, _
Optional ByVal myFac As Double = 1#) As Double
RoundtoUp = (Fix(myNum * CDec(10# ^ myFac)) + IIf(myNum = Fix(myNum * CDec(10# ^ myFac)), 0, Sgn(myNum))) / CDec(10# ^ myFac)
End Function
[TABLE="width: 655"]
<tbody>[TR]
[TD]Number[/TD]
[TD]Digits[/TD]
[TD][/TD]
[TD]RoundtoUp[/TD]
[TD]RoundUp (Excel)[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]1[/TD]
[TD]Ok[/TD]
[TD]'0.1[/TD]
[TD]'0.1[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]2[/TD]
[TD]Ok[/TD]
[TD]'0.01[/TD]
[TD]'0.01[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]3[/TD]
[TD]Ok[/TD]
[TD]'0.01[/TD]
[TD]'0.01[/TD]
[/TR]
[TR]
[TD]0.0099,[/TD]
[TD]4[/TD]
[TD]Error[/TD]
[TD]'0.01[/TD]
[TD]'0.0099[/TD]
[/TR]
[TR]
[TD]-0.0714285714,[/TD]
[TD]2[/TD]
[TD]Ok[/TD]
[TD]'-0.08[/TD]
[TD]'-0.08[/TD]
[/TR]
[TR]
[TD]-1.2345,[/TD]
[TD]3[/TD]
[TD]Ok[/TD]
[TD]'-1.235[/TD]
[TD]'-1.235[/TD]
[/TR]
[TR]
[TD]1.01234012340125,[/TD]
[TD]14[/TD]
[TD]Error[/TD]
[TD]'1.01234012340126[/TD]
[TD]'1.01234012340125[/TD]
[/TR]
</tbody>[/TABLE]
Could not debug the reason for such a behavior. What is a possible fix?
Last edited: