View Full Version : Money calculations
Stephen G.
04-19-2004, 12:30 PM
Hi guys,
I am trying to calculate money.
for example:
price = 34.99
shipping = 3.00
tax = 0.0825
I want the total calculated amount to be only 2 decimal places to the right (round off) so that it shows $41.12 instead of 41.120
My problem is that it calculates 3 or 4 decimal places to the right. Is there a way to fix this?
Thank you,
Stephen
This should do it.
price = 34.99;
shipping = 3.00;
tax = 0.0825;
total = price + ((price + shipping) * tax) + shipping;
-- number of digits after the decimal you want to show
digits = 2
shift = 10 ^ digits
result = Math.Floor(total * shift + 0.5 ) / shift
Dialog.Message("Rounded Total", "The rounded result is: " ..result)
Hi guys,
I am trying to calculate money.
for example:
price = 34.99
shipping = 3.00
tax = 0.0825
I want the total calculated amount to be only 2 decimal places to the right (round off) so that it shows $41.12 instead of 41.120
My problem is that it calculates 3 or 4 decimal places to the right. Is there a way to fix this?
Thank you,
Stephen
Stephen G.
04-19-2004, 08:39 PM
Thank you for your help. I really appreciate you taking your time to write this for me. It works great!!
Stephen
Glad to help.
Thank you for your help. I really appreciate you taking your time to write this for me. It works great!!
Stephen
csd214
04-20-2004, 02:50 AM
Amazing Worm, you always have a simple solution.
I have just learned (from support) that the input mask is not suitable for variable amounts. The mask “###,###.##” requires an input of 6+2 digits. If the user inputs “123.45” the result is not good at all. The mask can have the decimal placeholder and the thousands separator specified in the user’s international settings, but that’s of no help to the amount issue.
I’m new to AMS5; it is indeed incredible what this app can do. At this stage I’m considering how I could take advantage of AMS5. My experience is commerce and economics applications. To learn how to handle with tables, I have started a “Database Project” with normal single column tables and associative tables. The project comprises string and numeric values (amounts) and should read from and write to a .csv file (to be used with Excel).
With my regional settings Excel uses the format ’12345,67’ when the value is ‘12345.67’. Inside AMS the decimal point has to be the dot (to perform calculations), but it is of course possible to convert the string “12345,67” to numeric 12345.67, and the regional decimal symbol and thousand separator can be read from the Registry.
The AMS5 limitations seem to be:
1) Restrict input to numeric values in input boxes
2) Perform data validation when the users leaves the input object
3) Display formatted numeric values (amounts) like “12 345,67” (in US “12,345.67”).
The restriction issue can be performed by a validation script, but when? I miss the Lost Focus Event. It is not a problem to fire the validation code when the user moves from field to field by pressing Enter or Tab, but it’s trickier to handle the mouse movements. The display option can be done by a global function with notice to the regional settings. [Q: Does anybody have a script like that?].
I’m aware of the SQLLite Plug-In, but [Q:] I presume that this add-on will NOT give me any new input object (to handle amounts)? As soon as the database is created, I think SQLLite will be most efficient to query the database.
Don't be too impressed. The answer above is the result of a Google Search. There are a lot of LUA scripts out there and I'm a firm believer in NOT re-inventing the wheel :)
Amazing Worm, you always have a simple solution.
csd214
04-24-2004, 04:25 AM
The formula “Math.Floor(value * shift + 0.5) / shift” isn’t reliable when the value is near zero. If value = 1.005, 1 is returned (expected 1.01). -7.005 gives -7, but -8.005 returns -8.01. If you are more an artist than an accountant, you probably don’t care?
“Floor” isn’t exactly the same as “Integer”. I prefer to round values in the traditional way, that is using the Integer Function. I haven’t found the action Math.Int, but thanks to the global function feature, it’s easy to create your own function Int().
To make rounding simple, the function Round() (with parameters ‘value’ and ‘decimals’) is handy; see code below.
If you; ore more precisely your users; are living outside “the dot area”, you have more serious challenges. With “the dot area” I’m speaking of US, Canada, UK ++. There is a lot of countries using comma as the decimal point. When the numeric key pad has comma bundled with the decimal point key, you can’t ask your users to use the punctuation key when they are entering a numeric value.
I’m living in a “comma country”, but with the functions Value(), Numeric() and Amount() in my toolbox, I’m comfortable with AMS5 and accounting apps too, and Excel has no problem with understanding .csv files created in AMS5. (Excel takes notice of the regional settings.)
Are there more comma hackers out there? I should like to gain knowledge of your experiences.
function Int(_entry)
local decpPos = String.Find(_entry, ".", 1, false);
if decpPos ~= - 1 then
_entry = String.Left(_entry, decpPos - 1);
end
return _entry
end --=============== Int(_entry) ================
function Round(_value, _decimals)
_value = String.ToNumber(_value); _decimals = String.ToNumber(_decimals);
local shift = 10 ^ _decimals;
if _value >= 0 then
_value = (Int(_value * shift + 0.5)) / shift;
else
_value = (Int(_value * shift - 0.5)) / shift;
end
return _value
end --================ round(_value) ===================
Nice!
Now get that out there so when I do my Google searches, I find yours! :D
The formula “Math.Floor(value * shift + 0.5) / shift” isn’t reliable when the value is near zero. If value = 1.005, 1 is returned (expected 1.01). -7.005 gives -7, but -8.005 returns -8.01. If you are more an artist than an accountant, you probably don’t care?
“Floor” isn’t exactly the same as “Integer”. I prefer to round values in the traditional way, that is using the Integer Function. I haven’t found the action Math.Int, but thanks to the global function feature, it’s easy to create your own function Int().
To make rounding simple, the function Round() (with parameters ‘value’ and ‘decimals’) is handy; see code below.
If you; ore more precisely your users; are living outside “the dot area”, you have more serious challenges. With “the dot area” I’m speaking of US, Canada, UK ++. There is a lot of countries using comma as the decimal point. When the numeric key pad has comma bundled with the decimal point key, you can’t ask your users to use the punctuation key when they are entering a numeric value.
I’m living in a “comma country”, but with the functions Value(), Numeric() and Amount() in my toolbox, I’m comfortable with AMS5 and accounting apps too, and Excel has no problem with understanding .csv files created in AMS5. (Excel takes notice of the regional settings.)
Are there more comma hackers out there? I should like to gain knowledge of your experiences.
function Int(_entry)
local decpPos = String.Find(_entry, ".", 1, false);
if decpPos ~= - 1 then
_entry = String.Left(_entry, decpPos - 1);
end
return _entry
end --=============== Int(_entry) ================
function Round(_value, _decimals)
_value = String.ToNumber(_value); _decimals = String.ToNumber(_decimals);
local shift = 10 ^ _decimals;
if _value >= 0 then
_value = (Int(_value * shift + 0.5)) / shift;
else
_value = (Int(_value * shift - 0.5)) / shift;
end
return _value
end --================ round(_value) ===================
Corey
04-24-2004, 07:31 AM
Comma, comma, comma, comma, comma, chameleon... :o
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.