![](/img/trans.png)
[英]How to format number with “.” as thousand separator, and “,” as decimal separator?
[英]Thousand Separator in DBGrid
我正在使用MySQL和Delphi XE4来构建应用程序。 数据库中有很多数字,我在查询中使用FORMAT函数来应用千位分隔符!
SELECT Field1,FORMAT((Field2 * Coef), 0) AS blabla FROM MyTable WHERE .....
一切都很好,但是当我尝试在FastReprot中添加SUM函数时,发生了逻辑错误!
FORMAT函数以字符串形式返回结果,SUM函数将字符串连接起来!
SUM(frxDBDataset1."blabla",MasterData1)
Product1| 123,500,000
Product2| 1,455,999,100
________________________________
SUM = 123,500,000-1,455,999,100
在DBGrid中显示千位分隔符的最佳方法是什么? 我正在考虑在“ OnGetText”事件中应用千位分隔符。 当recordcount> 5000时,此方法是否会导致在DBGrid中显示数据的延迟?
你有什么建议吗?
在查询中格式化结果的格式确实是一个坏主意。 该查询应用于检索信息...表示应在其他地方处理。 在这种情况下,我将按照注释中的建议使用(实际上,我确实使用了)DisplayFormat。
如果您不总是希望在设计时将字段添加到TDataSet中,则还可以编写一些代码来自动格式化:
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;
这应该适合您的实际需求...例如,您可以从BD(取决于BD)读取元数据来决定精度。
在FastReport中,您也可以使用DisplayFormat,或者还可以在备忘录中使用format函数: [Format('%.2n', [<DataSet."Field">])]
,其中.2
表示使用。
编辑:为了解决出现在与右侧的减号的问题BiDiMode = bdRightToLeft
(见本 ),我相信你会真正需要使用OnGetText
事件。 我认为您已经自己找到了解决方法,但是为了完整起见,我还是将其包括在内:
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;
稍作更改,效果很好。 [ 文本:= FormatFloat(FmtStr,-F)+'-'; ]
特别感谢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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.