I'm using MySQL and Delphi XE4 to build an application. There are big numbers in Database and I used FORMAT function in my queries to apply Thousand Separator!

SELECT Field1,FORMAT((Field2 * Coef), 0) AS blabla FROM MyTable WHERE .....

Everything was fine but When I tried to add SUM function in FastReprot an logical error happend!

FORMAT function returns the result as a string and SUM function concatenates strings!!!

SUM(frxDBDataset1."blabla",MasterData1)

Product1| 123,500,000
Product2| 1,455,999,100
________________________________
SUM = 123,500,000-1,455,999,100

What is best way for showing thousand separator in DBGrid! I'm thinking about applying thousand separator in "OnGetText" Event. Does this method cause delay in showing data in DBGrid when recordcount>5000 ?

Do you have any suggestions?

有帮助吗?

解决方案

It's really a bad idea to format your results inside the query. The query should be used to retrieve information... presentation should be handled elsewhere. In this case, I would use (I do use, in fact) the DisplayFormat, as suggested in comments.

You can also write some code to format automatically, if you don't always want to add the fields to the TDataSet on design-time:

DataSet.Open;
For I := 0 To DataSet.Fields.Count - 1 Do Begin
    If DataSet.Fields[I] Is TFloatField Then
        (DataSet.Fields[I] As TNumericField).DisplayFormat := '###,###,##0.00'
    Else If DataSet.Fields[I] Is TIntegerField Then
        (DataSet.Fields[I] As TNumericField).DisplayFormat := '###,###,##0';
End;

This should be adapted to your actual needs... you could read metadata from BD (this is BD-dependent) to decide on precision, for example.

Inside FastReport, you could also use DisplayFormat, or you can also use the format function inside your memo: [Format('%.2n', [<DataSet."Field">])], where the .2 indicates the precision to be used.

EDIT: To address the problem of the minus sign appearing on the right with BiDiMode = bdRightToLeft (See this), I believe you'll really need to use OnGetText event. I think you already found this workaround yourself, but I'm including it nevertheless for completeness:

procedure TForm1.FieldGetText(Sender: TField; var Text: String; DisplayText: Boolean);
var
  FmtStr: string;
  F: Double;
begin
  F := Sender.AsFloat;
  FmtStr := TNumericField(Sender).DisplayFormat;
  if Sign(F) = -1 then
    Text := '-' + FormatFloat(FmtStr, Abs(F))
  else
    Text := FormatFloat(FmtStr, F)
end;

其他提示

With a little change it worked very well. [Text := FormatFloat(FmtStr, -F) + '-';]

Special thanks to GabrielF.

procedure TForm1.FieldGetText(Sender: TField; var Text: String; DisplayText: Boolean);
var
  FmtStr: string;
  F: Double;
begin
  F := Sender.AsFloat;
  FmtStr := TNumericField(Sender).DisplayFormat;
  if F < 0 then
    Text := FormatFloat(FmtStr, -F) + '-';
  else
    Text := FormatFloat(FmtStr, F)
end;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top