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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21399729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:11:00  来源:igfitidea点击:

Delphi TFloatField.DisplayFormat for numeric fields less than 1.0

databasedelphistring-formattingvcltfield

提问by user3197381

This is my procedure.

这是我的程序。

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;

This is work fine until a number like "0.9" has been entered and result will be ".9". How can I have thousand separator and zero before floating point that smaller than "1".

在输入“0.9”之类的数字并且结果为“.9”之前,这是正常工作的。如何在小于“1”的浮点数之前有千位分隔符和零。

采纳答案by Hugh Jones

The format you need is ###,##0.0#

你需要的格式是 ###,##0.0#

回答by Arioch 'The

Try (Fields[i] as TFloatField).DisplayFormat := '##0,000.00';

尝试 (Fields[i] as TFloatField).DisplayFormat := '##0,000.00';

As you did read in documentation at http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Default_Formatting_for_Numeric,_Date,_and_Time_Fieldsit says

正如您在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

默认格式化由以下例程执行:

  • FormatFloat -- TFloatField, TCurrencyField

And how you did read in the following documentation pages

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

the documentation quotes

文档引用

  • 0 -> Digit placeholder. If the valuebeing formatted has a digitin the position where '0' appears in the format string, then that digit is copied to the output string. Otherwise, a '0' is storedin that position in the output string.
  • # -> Digit placeholder. If the valuebeing formatted has a digitin the position where '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is storedin that position in the output string.
  • 0 -> 数字占位符。如果正在格式化的值在格式字符串中出现“0”的位置有一个数字,则该数字将被复制到输出字符串中。否则,“0”将存储在输出字符串中的该位置。
  • # -> 数字占位符。如果正在格式化的值在格式字符串中出现“#”的位置有一个数字,则该数字将被复制到输出字符串中。否则,输出字符串中的该位置不会存储任何内容

So by using "#" in the formatting pattern you tell Delphi "i do not need any digits (and thousands separators with them) in this place, but you might put them if you want" - and since Delphi does not want to put leading zeros - you don't have any. However, if you really need those digits and the thousands separator with them, you put "0" instead of "#" and that way you tell Delphi "the digits just need to be here, whether you want to put them or not"

因此,通过在格式化模式中使用“#”,您可以告诉 Delphi“我在这个地方不需要任何数字(和千位分隔符),但是如果你愿意,你可以把它们放在里面”——而且因为 Delphi 不想把前导零 - 你没有。但是,如果你真的需要这些数字和千位分隔符,你可以用“0”代替“#”,这样你就告诉 Delphi“数字只需要在这里,无论你想不想放它们”