Thread: Data Type problems in my Rounding function

1. Registered User
Join Date
Jul 2004
Posts
156

Unanswered: Data Type problems in my Rounding function

Season's Greetings, y'all!

I have a function to round my overtime payrates to 3 decimal places after a 1.5*RegularPay.

Code:
```Function RoundUp(ByVal Number As Variant, DecPlaces As Long) As Variant
RoundUp = (Int((Number + 0.5 / 10 ^ DecPlaces) * 10 ^ DecPlaces) / 10 ^ DecPlaces)
End Function```
The field (this is a bound form) is in Currency data type. Using Variant, sometimes (and ONLY sometimes) the function will just not round correctly. The above code using Number = 24.4215 at 3 decimal places would give me 24.421. When following the code step by step, it says that the Int() portion above would give me 24421....which makes absolutely no sense.

However, when I change the Number variable to Currency above like so

Code:
```Function RoundUp(ByVal Number As Currency, DecPlaces As Long) As Variant
RoundUp = (Int((Number + 0.5 / 10 ^ DecPlaces) * 10 ^ DecPlaces) / 10 ^ DecPlaces)
End Function```
it works like a champ. Does anyone have any suggestions?

Okay, I'm just going to leave this up for FYI purposes only and to ask if anyone can explain to me WHY I just changed it back to variant to test it and it WORKED like it SHOULD...... Explanations or should this go to the X-files? Thanks!

Join Date
Nov 2004
Location
out on a limb
Posts
13,692
have you read the help file documentation on the various arithmetic functions and understood the principle behind 'bankers rounding rules' might be worht a try

3. Registered User
Join Date
Jul 2004
Posts
156
Yeah, I like "might be worth a try" over "thought not," too.

Actually, yes, I understand bankers' rounding rules. The "don't round if even" and "do round if odd" when 5 rule. The bankers' rules applies to the Round function--not the simple function composed above. This does not apply to the Int function. The above code was used on the same number and it gave two different answers. This was totally random. I don't see it being in documentation since it was the same code used each time with the same value. Nothing changed. So, this wasn't an error in how the code was typed. It had to be in how it was run. If you can explain that to me, I'll let you by with the "thought not" remark. j/k!

4. Cavalier King Charles
Join Date
Dec 2002
Location
Préverenges, Switzerland
Posts
3,740
does this explain it ?

izy

5. Registered User
Join Date
Jul 2004
Posts
156
It could. It's just that I used the same number, same function, same everything. I just happened to try it once more using Variant in both datatype declarations and it started giving me the expected number.

I think I'll just take away declarting the function as Variant and set it to Currency since that's the field datatype anyways. You know, I never knew floating point was such a headache for programmers. I always thought numbers were something that processor engineers and programming language engineers had gotten down pat. But it's all becoming clear as to why this IS a problem. So, what we need quantum processors and get rid of this binary crap.

Thanks, guys! I guess I'll just start using the "@" symbol that MS suggested in that article or convert all my number fields to Currency. Good idea?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•