Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Currency data type, BUG or FEATURE?
Message
From
09/03/2006 21:20:11
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
09/03/2006 20:14:05
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01102959
Message ID:
01102962
Views:
15
Almost any calculation that involves currency will convert all intermediate results to currency (4 decimals). Repeat this with a calculator, and see what you get - rounding all intermediate results to 4 digits. You even get different results if you change the order of the factors, putting the currency value at the end.

So, if you need to calculate with more decimals, yes, you need some conversion. Instead of cast(), you can also use mton(). By the way, an alternative for repro code is to assign a constant with $:
UnitPrice = $35760
...
I would not consider this a bug (although it can cause nasty bugs in your own code, if you don't remember it!) - that is the way currency data type is inherently handled. With currency data type, addition and subtraction is inherently exact; multiplication and division is not.

Saludos,

Hilmar.

>Hi all,
>
>I have built the following simple repro code of something that is driving me crazy:
>
>
>create Cursor temp (unitPrice Y, quantity B(5), discount B(5))
>
>insert into temp values (35760, 10, 28.221)
>
>? UnitPrice                 * quantity * (1 - (discount/100))    && displays 256685.28
>? cast(UnitPrice as double) * quantity * (1 - (discount/100))    && displays 256681.704
>
>
>There is a difference of almost 4 between the 2 calculations. I have a lot of code that makes calculations, and if I have to CAST every bit of it, it's going to take quite a time. Is there a simple solution to this? Is this a BUG or a FEATURE?
>
>Thanks a lot,
>Javier.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform