AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Currency type

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
David Andersen



Joined: 15 Jul 2005
Posts: 140
Location: Denmark

PostPosted: Fri Oct 30, 2009 9:52 am    Post subject: Currency type Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Fri Oct 30, 2009 10:01 am    Post subject: Reply with quote

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
View user's profile Send private message
David Andersen



Joined: 15 Jul 2005
Posts: 140
Location: Denmark

PostPosted: Fri Oct 30, 2009 10:05 am    Post subject: Reply with quote

The problem is that floating point operations are inaccurate. I believe the following page explains why:
http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm
Back to top
View user's profile Send private message Send e-mail Visit poster's website
shabj
Guest





PostPosted: Fri Oct 30, 2009 10:20 am    Post subject: Reply with quote

David Andersen wrote:
The problem is that floating point operations are inaccurate. I believe the following page explains why:
http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm

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

PostPosted: Fri Oct 30, 2009 11:38 am    Post subject: Reply with quote

Excellent, shabj. It seems to solve my problem. Hope these posts make it easier for others to find.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
David Andersen



Joined: 15 Jul 2005
Posts: 140
Location: Denmark

PostPosted: Fri Oct 30, 2009 1:02 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Sat Oct 31, 2009 4:47 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group