Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While dividing/multiplying currency -- wrong result....

Status
Not open for further replies.

peter37

Programmer
Jan 20, 2006
10
CA
Hi!
I am doing calculation during my project .... and i am operating with currency values... unless i transform currency to numeric ( using MTON()) it's giving me wrong result !!
example :
x=$480.00
y=$26379.87
z=$1033.80

result=x/y*z = 18.8152 -- in VFoxPro (version 6)
18.8107 -- calculator...

Known bug ? or am i doing something wrong ?

Thanks ...
 
With currency calculations only 4 decimal places are maintained; With normal numbers, 10 dec. places.

This lack of precision is causing the currency calculations to come out wrong.

Try this example:
Code:
?"x=$480.00"
?"y=$26379.87"
?"z=$1033.80"
?
?"With currency:"
x=$480.00
y=$26379.87
z=$1033.80
do docalc
?"With regular numbers:"
x=480.00
y=26379.87
z=1033.80
do docalc

proc docalc
a=x/y
?"x/y="
??a
?"a/z="
??a/z
?"x/y*z"
?x/y*z
result=x/y*z      
?"result"
?result
?"= 18.8152 -- in VFoxPro (version 6)"
?"  18.8107 -- calculator..."
 
I'm curious what does $ times $ give you? "square" $'s? Wouldn't most calculations (other than straight +/-) be mixed mode?

Rick
 
HI
1. A currency value is always 4 decimals for VFP and so irrespective of SET decimals value.. the results are 4 decimals accurate.
2. In an integer.. if decimals are set to 4, and then if two numeric variables are multiplied or divided.. the decimals can go more number of digits. Depending on SET FIXED, the actual value displayed could be different. Then if this 6 decimal or 8 decimal value is multiplied or divided again the number of decimals could be again different. HOwever, the currency variable will always have 4 digits.

So depending on the number of decimals.. this figure is different. Calculator does the computation as numeric figures showing more number of digits.

In the above example..
($480/$26379.87)=0.0182
(480/26379.87) =0.01819569
IF "set fixed is 4" then (480/26379.87)=0.0182 .. the same as $ values.

So this explains your concern.
Hope this helps you to understand the VFP behaviour.
:)
ramani :-9
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
HI
1. A currency value is always 4 decimals for VFP and so irrespective of SET decimals value.. the results are 4 decimals accurate.
2. In an integer.. if decimals are set to 4, and then if two numeric variables are multiplied or divided.. the decimals can go more number of digits. Depending on SET FIXED, the actual value displayed could be different. Then if this 6 decimal or 8 decimal value is multiplied or divided again the number of decimals could be again different. HOwever, the currency variable will always have 4 digits.

So depending on the number of decimals.. this figure is different. Calculator does the computation as numeric figures showing more number of digits.

In the above example..
($480/$26379.87)=0.0182
(480/26379.87) =0.01819569
IF "set fixed is ON" then (480/26379.87)=0.0182 .. the same as $ values.

So this explains your concern.
Hope this helps you to understand the VFP behaviour.
:)
ramani :-9
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
Hi, your have an other way to get the desired precision using the currency value (p.ex when you've an unit price and a quantity to obtain the correct total price). Use the mton() function to convert your creency values to double !
Whis your exemple, use mton(x)/mton(y)*mton(z) the result will be 18.81070680

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top