| View previous topic :: View next topic |
| Author |
Message |
David Andersen
Joined: 15 Jul 2005 Posts: 140 Location: Denmark
|
Posted: Fri Oct 30, 2009 9:52 am Post subject: Currency type |
|
|
| I want to store currency in a variable and do various math functions on it. As you probably know, using float variables are inprecise and therefore, other languages have introduced decimal types. What is the alternative in Autohotkey. I could of course simply remove the comma, do the math and reintroduce the comma after the calculation, but there must be another way. |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Fri Oct 30, 2009 10:01 am Post subject: |
|
|
If your currency amounts contain commas, they won't be recognized as numbers by AHK. You'll need to remove the commas (StringReplace), then use ordinary floating point operations on the resulting number. Afterwards, you can reintroduce formatting. (also see SetFormat)
So, I don't know of anything special in AHK for dealing with currency. _________________ Ternary (a ? b : c) guide TSV Table Manipulation Library
Post code inside [code][/code] tags! |
|
| Back to top |
|
 |
David Andersen
Joined: 15 Jul 2005 Posts: 140 Location: Denmark
|
|
| Back to top |
|
 |
shabj Guest
|
Posted: Fri Oct 30, 2009 10:20 am Post subject: |
|
|
Have a look at this arbitrary precision library. long integer arithmetic library . you will still have to deal with any punctuation. |
|
| Back to top |
|
 |
David Andersen
Joined: 15 Jul 2005 Posts: 140 Location: Denmark
|
Posted: Fri Oct 30, 2009 11:38 am Post subject: |
|
|
| Excellent, shabj. It seems to solve my problem. Hope these posts make it easier for others to find. |
|
| Back to top |
|
 |
David Andersen
Joined: 15 Jul 2005 Posts: 140 Location: Denmark
|
Posted: Fri Oct 30, 2009 1:02 pm Post subject: |
|
|
| I had a look at the arbitrary precision library, though I cannot see that it helps in any way, if I still have to convert from the string with decimals to a float. |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Sat Oct 31, 2009 4:47 am Post subject: |
|
|
In AHK, variable contents are strings (the official explanation is in the documentation) in most cases. You don't actually 'convert' a string of numbers into a different variable type, AHK attempts to use the string as though it were a number when it is used in a math expression.
This means that you'll have no trouble with code like the following: | Code: | pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
MP_Mul( 2pi, pi, 2 )
msgbox %2pi% ; this would display 6.283... out to 100 decimal places |
And you really should look at the SetFormat docs page, as it explicitly details AHK's math limits and features. | The Manual wrote: | | In v1.0.48+, floating point variables have about 15 digits of precision internally unless ... |
_________________ Ternary (a ? b : c) guide TSV Table Manipulation Library
Post code inside [code][/code] tags! |
|
| Back to top |
|
 |
|