database - 小于 1.0 的数字字段的 Delphi TFloatField.DisplayFormat

标签 database delphi string-formatting vcl tfield

这是我的程序。

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

以及您是如何阅读以下文档页面的

文档引用

  • 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/

相关文章:

common-lisp - 左对齐零填充

字段、EAV 等中的 MySQL 多个值

.net - 比 MS Access 2007 更快的东西,快速可移植数据库推荐?

delphi - bool 值的类默认属性设置为 true 在运行时给出 false

python - 为什么 Python 字符串格式会默默地忽略与类实例不匹配的参数计数?

Python:将文本格式化为特定列

sql - 为什么 SQL 连接在 Oracle 中失败?

java - 在 java derby db 中执行选择请求时如何将磁盘 100 减少到最小使用量

Delphi 7 DCC32 如何指定.exe名称?

Delphi 构建自动化和持续集成仅使用 MS Build 可行吗?