Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread  TBCD / TCurrency bug ....Any ideas?
Thu, Nov 7 2019 10:21 AMPermanent Link

kamran

Hello

My environment: Delphi Berlin(latest update, DBisam(Latest update), Windows 10.

So I am having an issue with a simple calculation:

I have defined a DBisam field as BCD, size 2

In code I perform a simple multipication.

To keep it simple and to illustrate I am using numbers in the assignment but other variations with fields also display incorrect value.  

***
datafrm.tbTranDetail.FieldByName('TD_TRAN_PRODUCT_SELLING_PRICE_INC_VAT').AsBCD:=2.98 * 1.20;

showmessage(BcdToStr(datafrm.tbTranDetail.FieldByName('TD_TRAN_PRODUCT_SELLING_PRICE_INC_VAT').AsBCD));

// THIS EVALUATES TO 3.57 ... BUT SHOULD BE 3.58

***

I hear that there are issues with the TBCD, TCurrency types across different versions of Delphi compilers.
( I am unable to test this as I am on Delphi Berlin )

I chose BCD as its supposed to be more accurate;
I would expect that to be the case as they are of a financial nature.

Any work around or suggestions ?

best regards

Kamran
Thu, Nov 7 2019 10:45 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

kamran


If your field is currency then one common approach is to store the amount in then minor unit ie in your case rather than hold 3.58 you'd hold 358. The other thing you could do is carry out the rounding yourself, even going back into the dim and distant past where the approach was add 0.5 and truncate

I was going to say it was probably due to bankers rounding but refreshing my memory says that's not the case, but you might like to have a look at this

http://docwiki.embarcadero.com/Libraries/Rio/en/System.Math.SetRoundMode

just in case it has relevance

Roy Lambert
Fri, Nov 8 2019 7:50 AMPermanent Link

Walter Matte

Tactical Business Corporation

If you want more accuracy use more decimals and control your rounding when you want to output less decimals.

There is no perfect solution.

Walter
Fri, Nov 8 2019 1:58 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate

kamran,

It's important that you post the exact code and types and not a simplification.
Aslo, not knowing the exact details, I suggest that you try FieldByName(...).AsCurrency instead of .AsBCD and see what happens.

--
Fernando Dias
[Team Elevate]
Wed, Nov 13 2019 9:09 AMPermanent Link

kamran

Hi All

Thanks for suggestions.. I will dig into further. Thanks

Kamran
Image