-
Jan 24th, 2023, 08:05 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Calculate percent with Currency Data Type
hi,
i use Int(curData * 100 / curDataTotal) to calculate the current percent progress value but i run into an overflow error if curData*100 is higher than the maximum currency value (922337203685477@).
Does anybody have an idea for a different approach to calculate the percent progress value with large currency variables to avoid the overflow error?
-
Jan 24th, 2023, 08:07 PM
#2
Re: Calculate percent with Currency Data Type
Originally Posted by Mith
hi,
i use Int(curData * 100 / curDataTotal) to calculate the current percent progress value but i run into an overflow error if curData*100 is higher than the maximum currency value (922337203685477@).
Does anybody have an idea for a different approach to calculate the percent progress value with large currency variables to avoid the overflow error?
Code:
Int((curData / curDataTotal) * 100)
-
Jan 24th, 2023, 08:58 PM
#3
Re: Calculate percent with Currency Data Type
That forward-slash division is going to return a Double, which doesn't have the significand precision that a Currency has. In other words, down in the "little numbers" you may see zeroes start appearing.
ADDED: In other words...
Code:
Debug.Print TypeName(CCur(1)/CCur(2))
... reports "Double".
Without an API call, VB6 doesn't give us a way to do integer division with Currency types.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 24th, 2023, 09:02 PM
#4
Re: Calculate percent with Currency Data Type
You could use Decimal (in a Variant) type to do it. Division with Decimal types is done with pure Decimals.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 24th, 2023, 09:10 PM
#5
Thread Starter
Hyperactive Member
Re: Calculate percent with Currency Data Type
After some tests i found a "strange" solution to avoid the overflow error:
Int((curData/1) * 100 / (curDataTotal/1))
example: Int((722337203685477@/1) * 100 / (922337203685477@/1)) = 78%
Can someone explain why dividing by 1 avoids the overflow error?
And finally this solution works very nice:
Int(curData / curDataTotal * 100)
-
Jan 24th, 2023, 09:13 PM
#6
Re: [RESOLVED] Calculate percent with Currency Data Type
You don't need the curData/1. All you need to do is CDbl(curData) because that's effectively what you're doing. Again, Double doesn't have as much significand as a Currency, so your "little numbers" may go to zero.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 24th, 2023, 09:15 PM
#7
Re: [RESOLVED] Calculate percent with Currency Data Type
A Double has 52 bits of significand, whereas a Currency has 63 bits of significand.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 24th, 2023, 09:24 PM
#8
Thread Starter
Hyperactive Member
Re: [RESOLVED] Calculate percent with Currency Data Type
Originally Posted by Elroy
A Double has 52 bits of significand, whereas a Currency has 63 bits of significand.
ok, its a downgrade from currency to double and this avoids the error. thanks for the info!
i will use your first solution: Int((curData / curDataTotal) * 100)
-
Jan 24th, 2023, 09:28 PM
#9
Re: [RESOLVED] Calculate percent with Currency Data Type
Use Decimal for calculation and then use CCur() to get the currency back (or you get Error 6, overflow)
Decimal has priority. Multiplication/division for Currency turn to double, so change to decimal using Cdec(curVariable) and do the maths and then use CCur to get back the Currency. You get the maximum precision. Decimal is better than Currency, but need 16 bytes (as a type of a variant). Currency value need 8 bytes.
I will post the new cint64() as I prepare for my Interpreter (is the best solution).
-
Jan 26th, 2023, 11:02 AM
#10
Re: [RESOLVED] Calculate percent with Currency Data Type
It's been bugging me that I didn't do a better job of explaining what I meant by rounding of the "little numbers".
Here's a piece of code:
Code:
Option Explicit
Private Sub Form_Load()
Dim c As Currency
c = 864286428642864.2864@
Dim c2 As Currency
c2 = CDbl(c) / 2
Debug.Print Format$(c2, "#0.0000")
End Sub
Common sense tells us that the answer should be: 432143214321432.1432
But, when we run this, here's what we get:
Code:
432143214321432.1250
^^^
||| wrong
||
|| wrong
|
| wrong
And that happens without throwing any error at all. And the reason it happens is because we did the division in Double (which doesn't have as much significand as a Currency).
The easiest way to get this correct (as stated above) is to convert everything to Decimal (with CDec() function) before doing the division. And doing that will correctly throw overflow errors (if there is one) when the Decimal is re-assigned to the Currency.
JUST FYI: There is no Currency division. There's Currency addition, subtraction, & multiplication ... but no division. However, for Decimal (which completely covers all ranges of Currency) there is a pure Decimal division.
Last edited by Elroy; Jan 26th, 2023 at 11:15 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|