Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Currency calculations. Bug?
Message
From
30/03/2011 19:34:46
 
General information
Forum:
Visual FoxPro
Category:
Troubleshooting
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01505515
Message ID:
01505613
Views:
38
Hi Yuri.

What it's doing makes sense as a Numeric value multiplied by a Currency value produces a Currency value, and thus the rounding has to occur.

If you are going to use the currency data type you need to prevent premature conversion of values to Currency, so you need to either use parenthesis to make all of the numeric values get processed first or, as you saw, convert the Currency value to numeric for the calculation and let it be turned back into Currency when you save the value back.
?"Currency field: ", (n2/n1) * fldcur, fldcur * (n2/n1) && Values now match

Ian Simcock.



>Rounding is supposed to be applyed to Currency value, and it is OK.
>But it looks like rounding is applyed to each value in the expression no matter whether it is currency or not, and before the result is calculated !
>Are you comfortable with this?
>
>
>>It's rounding (to 4 decimal digits) issue. Compare
>>
>>SET DECIMALS TO 7
>>? n2/n1, NTOM(n2/n1), n2/n1 * fldcur, fldcur * n2/n1, fldcur * (n2/n1), n2 * fldcur / n1
>>? "Numeric (Round):    ", ROUND(n2/n1,4) * fldn, fldn * ROUND(n2/n1,4)
>>
>>
>>>It is expected that result of calculations may be slightly different depending on the initial precision, but the difference in tens units?
>>>Anyone can shed light on it?
>>>
>>>
>>>* Tested in versions VFP5, VFP7, VFP 9 and VFP 9 Sp1
>>>CLEAR
>>>CLOSE tables all
>>>CREATE CURSOR testcrs (fldn N(12,4), fldcur Y) 
>>>ln=300000
>>>
>>>INSERT INTO testcrs VALUES (ln,ln)
>>>n1=43.05
>>>n2=44.06
>>>
>>>?"Variable:         ", n2/n1 * ln, ln * n2/n1		&& ~same
>>>?"Numeric field:    ", n2/n1 * fldn, fldn * n2/n1	&& ~same
>>>
>>>?"Currency field:   ", n2/n1 * fldcur, fldcur * n2/n1     && 307050.0000,        307038.3275        different 
>>>?"Currency2Numeric: ", n2/n1 * MTON(fldcur), MTON(fldcur) * n2/n1  && ~same
>>>
>>>
Previous
Reply
Map
View

Click here to load this message in the networking platform