Normal Topic format math formula (Read 824 times)
debmckee
Full Member
***
Offline



Posts: 123
Joined: Oct 5th, 2011
format math formula
Dec 21st, 2011 at 7:43pm
Print Post Print Post  
This is my (very simple) code as I am really new.
var vins as int
var vcent as int
var vamount as int

vins = (purchase$$ + of4) * .10              //purchase plus freight x 10%
vamount =  vins + (purchase$$ + of4)           // above amount added to freight and purchase price
vcent =  vamount * .002                      //insurance calculation

MI5 = vcent


My "test" answer should be 110.72.  But it is returning 110.00
I am sooo close.  But don't what to do next.

  
Back to top
 
IP Logged
 
Steve_in_Texas
Senior Member
*****
Offline


No personal text

Posts: 893
Location: San Antonio
Joined: Feb 21st, 2004
Re: format math formula
Reply #1 - Dec 21st, 2011 at 7:54pm
Print Post Print Post  
Money values should be stores as 'double', not 'int' (integer)

Use the correct variable types and you should be good to go.

Steve
  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: format math formula
Reply #2 - Dec 21st, 2011 at 8:12pm
Print Post Print Post  
Actually, you should use type Money, not double.

Int means integer. An integer can only store whole numbers, so you are losing your decimal places.

Double is a floating point number. It can store lots of decimal places, but it is often not good for currency as its high level of precision can cause rounding errors.

Money is the type to use for money/currency. It can store up to four decimal places, but does a thing called fixed point math to preserve currency calculations.

You should also make sure that MI5 is bound to a field of type Money.

Code
Select All
var vIns as Money
var vCent as Money
var vAmount as Money
var vTotal as Money

	vTotal = purchase$$ + of4
	vIns = vTotal * .10		//purchase plus freight x 10%
	vAmount = vIns + vTotal		// above amount added to freight and purchase price
	vCent =  vAmount * .002		//insurance calculation
	MI5 = vCent  

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
debmckee
Full Member
***
Offline



Posts: 123
Joined: Oct 5th, 2011
Re: format math formula
Reply #3 - Dec 21st, 2011 at 8:14pm
Print Post Print Post  
Thanks people!!  You are wonderful.....
  
Back to top
 
IP Logged
 
Steve_in_Texas
Senior Member
*****
Offline


No personal text

Posts: 893
Location: San Antonio
Joined: Feb 21st, 2004
Re: format math formula
Reply #4 - Dec 21st, 2011 at 8:27pm
Print Post Print Post  
Quote:
Actually, you should use type Money, not double.


Good to know. Thanks Erika!
  
Back to top
IP Logged
 
Rick_R
Full Member
***
Offline



Posts: 243
Joined: Jan 29th, 2010
Re: format math formula
Reply #5 - Dec 23rd, 2011 at 4:38am
Print Post Print Post  
One last point is that int does not do rounding. Setting an int variable to 3.999 will produce a value of 3, not 4.
  
Back to top
 
IP Logged