Results 1 to 10 of 10

Thread: [RESOLVED] Calculate percent with Currency Data Type

  1. #1

    Thread Starter
    Hyperactive Member Mith's Avatar
    Join Date
    Jul 2017
    Location
    Thailand
    Posts
    450

    Resolved [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?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: Calculate percent with Currency Data Type

    Quote Originally Posted by Mith View Post
    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)

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    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.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    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.

  5. #5

    Thread Starter
    Hyperactive Member Mith's Avatar
    Join Date
    Jul 2017
    Location
    Thailand
    Posts
    450

    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)

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    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.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    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.

  8. #8

    Thread Starter
    Hyperactive Member Mith's Avatar
    Join Date
    Jul 2017
    Location
    Thailand
    Posts
    450

    Re: [RESOLVED] Calculate percent with Currency Data Type

    Quote Originally Posted by Elroy View Post
    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)

  9. #9
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    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).

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    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
  •  



Click Here to Expand Forum to Full Width