Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/RTL/Delphi/Format + Float    [ Add a report in this area ]  
Report #:  79820   Status: Closed
Bug in FormatBcd() function
Project:  Delphi Build #:  14.0.3513.24210
Version:    14.0 Submitted By:   Peter Wolf
Report Type:  Basic functionality failure Date Reported:  11/26/2009 1:51:33 AM
Severity:    Commonly encountered problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   273810
Resolution: Fixed (Resolution Comments) Resolved in Build: : 16.0.4152.39274
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
FormatBcd() function outputs wrong result when the number on input is rounded up by the function and the the right padding with 0's is requested by the format used.

The result of the function is not right-padded with zeroes as compared to FormatFloat() function using the same number and format.

Added by Sysop
<<<<<<<<<
I should have used DoubleToBcd() function instead of CurrToBcd() in the demo of course, but it doesn't matter, the resut is the same.

And, This issue similar to QC#42792.
>>>>>>>>>
Steps to Reproduce:
This short demo reveals the bug:

uses
  Dialogs, FMTBcd;

procedure Reporoduce;
const
  Format = '0.00';
var
  AFloat: Double;
  ABcd: TBcd;
begin
  // Expected result: '1.90'
  AFloat := 1.895;
  CurrToBCD(AFloat, ABcd, MaxBcdPrecision, MaxBcdScale);
  ShowMessage(FormatFloat(Format, AFloat)); // output: '1.90'
  ShowMessage(FormatBcd(Format, ABcd));     // output: '1.9'

  // Expected result: '2.00'
  AFloat := 1.995;
  CurrToBCD(AFloat, ABcd, MaxBcdPrecision, MaxBcdScale);
  ShowMessage(FormatFloat(Format, AFloat)); // output: '2.00'
  ShowMessage(FormatBcd(Format, ABcd));     // output: '2'
end;
Workarounds
None
Attachment
None
Comments

Peter Wolf at 11/27/2009 1:13:54 AM -
I should have used DoubleToBcd() function instead of CurrToBcd() in the demo of course, but it doesn't matter, the resut is the same.

Tomohiro Takahashi at 11/27/2009 7:02:36 PM -
This issue similar to QC#42792.

Peter Wolf at 12/8/2009 1:10:50 AM -
I admit it's similar, but not the same. In addition the bug still persists in D2010, however QC#42792 was already fixed.

Dick Boogaers at 12/8/2009 4:17:07 AM -
The bug is in the function RoundAt in unit FMTbcd. In that function there is this code:

  if (Position < Length(Value)) and (DecPos < Position) then
  begin
    PP := PChar(Value) + ((Position -1));
    P := PP+1;
    if Byte(P^) >= 53  then    { if '5' or greater }
      PP^ := Char(Byte(PP^)+1);
    while (Position > 0) and ((Byte(PP^) > 57) or (PP^ = Dot)) do  {if greater than '9' then }
    begin
      if PP^ <> Dot then
        PP^ := '0';
      Dec(Position);
      Dec(PP);
      if PP^ <> Dot then
        PP^ := Char(Byte(PP^)+1);
    end;

Because the value of "Position" is needed later on to return the correct substring, it should not be overwritten in this loop.

The correct code goes something like this:

  if (Position < Length(Value)) and (DecPos < Position) then
  begin
    PP := PChar(Value) + ((Position -1));
    P := PP+1;
    if Byte(P^) >= 53  then    { if '5' or greater }
      PP^ := Char(Byte(PP^)+1);
    DecPos := Position;                                // <<< Copy Position to temp
    while (DecPos > 0) and ((Byte(PP^) > 57) or (PP^ = Dot)) do  // <<< Use temp i.s.o. Position
    begin
      if PP^ <> Dot then
        PP^ := '0';
      Dec(DecPos);                                   // <<< Use temp i.s.o. Position
      Dec(PP);
      if PP^ <> Dot then
        PP^ := Char(Byte(PP^)+1);
    end;

Server Response from: ETNACODE01