这是我的程序。
procedure format_integer_field(Atable: TDataSet);
var i: integer;
begin
if Atable.Active then
if Atable.FieldCount > 0 then
with Atable do
begin
for i:= 0 to FieldCount-1 do
if (Fields[i] is TIntegerField) then
begin
(Fields[i] as TIntegerField).DisplayFormat := '###,###';
(Fields[i] as TIntegerField).EditFormat := '#';
end
else
if (Fields[i] is TFloatField) then
begin
(Fields[i] as TFloatField).DisplayFormat := '###,###.##';
(Fields[i] as TFloatField).EditFormat := '#.##';
end;
end;
end;
在输入像“0.9”这样的数字并且结果将是“.9”之前,这是可以正常工作的。 如何在小于“1”的 float 前设置千位分隔符和零。
最佳答案
试试 (Fields[i] as TFloatField).DisplayFormat := '##0,000.00';
正如您在 http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Default_Formatting_for_Numeric,_Date,_and_Time_Fields 的文档中所读到的那样它说
Default formatting is performed by the following routines:
- FormatFloat -- TFloatField, TCurrencyField
以及您是如何阅读以下文档页面的
- http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.FormatFloat
- http://docwiki.embarcadero.com/Libraries/XE3/en/Data.DB.TNumericField.DisplayFormat
文档引用
- 0 -> Digit placeholder. If the value being formatted has a digit in the position where '0' appears in the format string, then that digit is copied to the output string. Otherwise, a '0' is stored in that position in the output string.
- # -> Digit placeholder. If the value being formatted has a digit in the position where '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is stored in that position in the output string.
因此,通过在格式化模式中使用“#”,您告诉 Delphi“我不需要任何数字(和千位分隔符)在这个地方,但如果需要,您可以放置它们”——因为 Delphi 不需要放置前导零 - 你没有。但是,如果你真的需要这些数字和千位分隔符,你可以用“0”代替“#”,这样你就可以告诉 Delphi“数字只需要在这里,不管你想不想放它们”
关于database - 小于 1.0 的数字字段的 Delphi TFloatField.DisplayFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399729/