AutoHotkey Community

It is currently May 26th, 2012, 10:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Currency type
PostPosted: October 30th, 2009, 10:52 am 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 30th, 2009, 11:01 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 30th, 2009, 11:05 am 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
The problem is that floating point operations are inaccurate. I believe the following page explains why:
http://effbot.org/pyfaq/why-are-floatin ... curate.htm


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 30th, 2009, 11:20 am 
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-floatin ... curate.htm

Have a look at this arbitrary precision library. long integer arithmetic library . you will still have to deal with any punctuation.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 30th, 2009, 12:38 pm 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
Excellent, shabj. It seems to solve my problem. Hope these posts make it easier for others to find.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 30th, 2009, 2:02 pm 
Offline

Joined: July 15th, 2005, 3:19 pm
Posts: 140
Location: Denmark
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 31st, 2009, 5:47 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], hyper_, JSLover, Kirtman, Leef_me, Maestr0, Miguel, XstatyK, Yahoo [Bot] and 60 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group