I have found a strange behavior of Delphi's FormatFloat function. Let me show the case study.
value to be converted : 129809.495
formatted output desired : 129,809.50
Case 1 : Converting from a string
var str: string;
str := '129809.495';
str := FormatFloat(',0.00', StrToFloat(str));
// output is 129,809.50 = CORRECT
Case 2 : Converting from a double variable
var number: double;
str: string;
val := 129809.495;
str := FormatFloat(',0.00', val);
// output is 129,809.50 = CORRECT
Case 3 : Converting from a dataset's field
*it's too complex to write here, let me just explain*
Basically the formatted output of FormatFloat(',0.00', Dataset.Field[0].AsFloat);
always resulted in 129,809.49 == WRONG
I've been testing this behavior using SQL Server 2008 & Firebird 1.5.
Component used are ADO Components and UniDAC components (by DevArt), and all have the same behavior.
I've tried to do these :
- FormatFloat(',0.00', Dataset.Field[0].AsFloat);
- FormatFloat(',0.00', StrToFloat(Dataset.Field[0].AsString));
- val := Dataset.Field[0].AsFloat; FormatFloat(',0.00', val);
- str := Dataset.Field[0].AsString; FormatFloat(',0.00', StrToFloat(str));
- val := StrToFloat(Dataset.Field[0].AsString); FormatFloat(',0.00', val);
But all resulted in same wrong conversion .49 instead of .50
1 way that works is
val := StrToFloat(Dataset.Field[0].AsString);
FormatFloat(',0.00', val);
Has anyone got a solution for this behavior? Because it'd be too much work to force convert StrToFloat and then reformat the variable/output. And this workaround can't be applied to 3rd party components that were using FormatFloat
Any help appreciated. Thanks