Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2004
    Posts
    131

    Star Money calculations

    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

  2. #2
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    This should do it.

    Code:
    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)
    Quote Originally Posted by Stephen G.
    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
    Last edited by Worm; 04-19-2004 at 12:09 PM.

  3. #3
    Join Date
    Feb 2004
    Posts
    131

    Thank you

    Thank you for your help. I really appreciate you taking your time to write this for me. It works great!!

    Stephen

  4. #4
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Glad to help.

    Quote Originally Posted by Stephen G.
    Thank you for your help. I really appreciate you taking your time to write this for me. It works great!!

    Stephen

  5. #5
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939

    Amounts format

    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.

  6. #6
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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

    Quote Originally Posted by csd214
    Amazing Worm, you always have a simple solution.

  7. #7
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939

    What’s on the floor?

    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.

    Code:
    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) ===================

  8. #8
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    Nice!

    Now get that out there so when I do my Google searches, I find yours!

    Quote Originally Posted by csd214
    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.

    Code:
    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) ===================

  9. #9
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    Comma, comma, comma, comma, comma, chameleon...

    Corey Milner
    Creative Director, Indigo Rose Software

Similar Threads

  1. Calculations
    By Jim Smiley in forum AutoPlay Media Studio 4.0
    Replies: 8
    Last Post: 10-27-2003, 09:12 AM
  2. Math Calculations
    By Incrocci in forum AutoPlay Media Studio 4.0
    Replies: 4
    Last Post: 07-31-2003, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts