Problem with FormatBcd

The offical for ZeosLib 7.3 Report problems, ask for help, post proposals for the new version of Zeoslib 7.3/v8
Quick Info:
-We made two new drivers: odbc(raw and unicode version) and oledb
-GUID domain/field-defined support for FB
-extended error infos of Firebird
-performance ups are still in queue
In future some more feature will arrive, so stay tuned and don't hassitate to help
Post Reply
Michl
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 01.04.2014, 12:53

Problem with FormatBcd

Post by Michl »

I see in viewtopic.php?f=50&t=131587&p=164419&hi ... CD#p164419, there is already a bug report for FPC FormatBcd. I have a similar problem, but not the same, so I create a new topic.

I changed a big project from old Zeos trunk version to current 8.0 patches branch. I use 64bit FPC 3.2.0 Lazarus Trunk on Windows 7 and communicate with PostgreSQL 10.

On a table I have a value NUMERIC. With this field I got a rangecheck error.

I debugged the issue and can see for a value 0,014 there this tBCD is set:

Code: Select all

  FillChar(BCD{%H-}, SizeOf(BCD), 0);
  BCD.Fraction[0] := 20;
  BCD.Precision := 2;
  BCD.SignSpecialPlaces := 3; 
With this value FormatBCD('#,###0.000', BCD) raise a range check error. If I use BCDToStr(BCD), the value is converted to a string.


If I use BCD := StrToBCD('0,014'), I can see this tBCD is set:

Code: Select all

  FillChar(BCD{%H-}, SizeOf(BCD), 0);
  BCD.Fraction[0] := 1;
  BCD.Fraction[1] := 64;
  BCD.Precision := 3;
  BCD.SignSpecialPlaces := 3;    
With this value FormatBCD('#,###0.000', BCD) and BCDToStr(BCD) are working fine.


If I use BCD := StrToBCD('0,14'), I can see this tBCD is set:

Code: Select all

  FillChar(BCD{%H-}, SizeOf(BCD), 0);
  BCD.Fraction[0] := 20;
  BCD.Precision := 2;
  BCD.SignSpecialPlaces := 2; 
This is mostly similar to first case and works for both functions FormatBCD('#,###0.000', BCD) and BCDToStr(BCD).


Now I don't know, if this is a bug in TZAbstractRODataset(DataSet).FResultSet.GetBigDecimal with a wrong comma position or is this BCD value valid and there is a bug in fpc FormatBCD or do I miss something?!


Simple test also a workaround patch added, so I can use Zeos for now.
You do not have the required permissions to view the files attached to this post.
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: Problem with FormatBcd

Post by EgonHugeist »

Hello Michl,

thanks for the detailed infos. I have fixed the bug on Zeos side, see r7388:https://sourceforge.net/p/zeoslib/code-0/7388/.
OTH it's definitelly a bug on FPC side too. Scale(SignSpecialPlaces) can't be greater than precision. Thus i would argue FPC FormatBCD is correct implemented wheras StrToBCD seems to be buggy (accept the FPC team argues "it's a feature" -> then FormatBCD should behave equal).

So please update from SVN and have fun! Next time please open a ticket on https://sourceforge.net/p/zeoslib/tickets/
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
Michl
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 01.04.2014, 12:53

Re: Problem with FormatBcd

Post by Michl »

Hello EgonHugeist,

thank you very much for your fast response and your hints! Sorry for late reply, I was struggling with a other issue, so I couldn't test this.

[Edit]
Next problem is fixed in Trunk! So you can ignore it!
[/Edit]


Not sure, if I should now open a bug tracker issue or post here?!

Now the app is starting correct and there is no exception anymore. But I encounter a new problem, IRC that wasn't before:

As I wrote above, I use

Code: Select all

GridData.Columns.Items[i].DisplayFormat := '#,###0.000';   
As I can see, this works fine for values <1 or >= 1000.
Values between this, show the thousend separator in front of it.

E.G.:
.026,000
.028,150
.013,000
.256,000
.001,325

When I debug the issue for e.g. 26 there the tBCD is set:

Code: Select all

  BCD.Fraction[0] := 0;
  BCD.Fraction[1] := 38;
  BCD.Precision := 4;
  BCD.SignSpecialPlaces := 0;    
, but should be:

Code: Select all

  BCD.Fraction[0] := 0;
  BCD.Fraction[1] := 38;
  BCD.Precision := 2;
  BCD.SignSpecialPlaces := 0;    
or

28,15:

Code: Select all

  BCD.Fraction[0] := 0;
  BCD.Fraction[1] := 40;
  BCD.Fraction[2] := 21;
  BCD.Precision := 6;
  BCD.SignSpecialPlaces := 2;    
FPC said, it should be:

Code: Select all

  BCD.Fraction[0] := 40;
  BCD.Fraction[1] := 21;
  BCD.Precision := 4;
  BCD.SignSpecialPlaces := 2;    
Last edited by Michl on 25.03.2021, 10:26, edited 1 time in total.
Michl
Fresh Boarder
Fresh Boarder
Posts: 13
Joined: 01.04.2014, 12:53

Re: Problem with FormatBcd

Post by Michl »

Hello EgonHugeist,

just tested trunk version. There is now everything fine!!

Thank you very much!
Post Reply