Round precision problem?
Hi!
I have noticed that if I format a double number with two decimal positions,
( 0.00 ), the round is not correct if latest decimal is "5".
For example, 15.225 is formatted like 15.22 not 15.23, so DBCalcs, Sums,
etc... are not correct.
With Delphi, FormatFloat returns correct result (15.23), so end-users can
see the correct value on screen, but when they print they get a different
Total value on the report!
With RAP, FormatFloat returns 15.22!
What's wrong?
Thanks!
Luis C.
I have noticed that if I format a double number with two decimal positions,
( 0.00 ), the round is not correct if latest decimal is "5".
For example, 15.225 is formatted like 15.22 not 15.23, so DBCalcs, Sums,
etc... are not correct.
With Delphi, FormatFloat returns correct result (15.23), so end-users can
see the correct value on screen, but when they print they get a different
Total value on the report!
With RAP, FormatFloat returns 15.22!
What's wrong?
Thanks!
Luis C.
This discussion has been closed.
Comments
When you make a call to FormatFloat from RAP, ReportBuilder simply calls
the Delphi FormatFloat routine and retrieves the value. In my testing,
the following code returns 15.22 in RAP and Delphi.
var
ldNumber: Double;
begin
ldNumber := 15.225;
Value := FormatFloat('0.00', ldNumber);
end;
ReportBuilder does not always use the FormatFloat routine to apply a
display format. Take a look at the TppDisplayFormat class located in
the ppDisplayFormat.pas file for how it is done. This class can be
replaced if needed to meet your needs.
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
AFAIK, 15.225 must be 15.23, not 15.22. At least in Spain and other Europe
countries.
Is it possible to change this behaviour without changing original RBuilder
source code? If not, I have to manually update the sources everytime I
install an update.
Thanks!
Santy C.
FormatFloat uses what is known as "Banker's Rounding" which rounds to
the nearest even number if half way between two numbers. Using Banker's
Rounding, 15.225 would round to 15.22 and Delphi is correct.
If you would like to alter this behavior you can use the
Math.SetRoundMode routine to force a round-up in this case.
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
The problem is with 'Double' DB fields.
With 'Extended' type fields the round is 'correct'.
I have to think if it is better to change DBPipeLine DataType from Double to
Extended, or simply modify "ppDisplayFormat.pas" source...
Thanks!