如何使用DisplayFormat在数字之间添加空格

问题描述 投票:2回答:3

如何使用DisplayFormat在数字之间添加空格。

像这个例子:

50130301037855000150550010000000131000000132

我需要这个:

5013 0301 0378 5500 0150 5500 1000 0000 1310 0000 0132

有人有想法吗?谢谢。

string delphi mask reportbuilder
3个回答
3
投票

您不能仅使用DisplayFormat属性来格式化,但可以依靠OnGetText事件来实现。

例如,假设您的字段是字符串类型:

function InsertBlankEach4Chars(S: string): string;
begin
  Result := '';
  while S <> '' do
  begin
    Result := Result + Copy(S, 1, 4) + ' ';
    Delete(S, 1, 4);
  end;
  Result := Trim(Result);
end;

procedure TMyForm.MyQueryFieldGetText(Sender: TField;
  var Text: string; DisplayText: Boolean);
begin
  if DisplayText then
    Text := InsertBlankEach4Chars(Sender.AsString)
  else
    Text := Sender.AsString;
end;

Disclaimer:此代码是在浏览器中编写的,未经测试。如果您打算在时间敏感的过程中使用此功能,我警告您可能会优化此功能以提高性能。


2
投票

我使用DisplayFormat找到了答案

9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999;0; 

谢谢。


1
投票

[如果您不想将其用于货币,请使用此。

procedure TForm1.Edit1Change(Sender: TObject);
var  a :Currency;
begin
ThousandSeparator :=' ';
if edit1.Text <>'' then  a:=strtofloat(edit1.Text) else a:=0;
edit2.Text :=FloatToStrF(a,ffNumber, 12, 2);
end;
© www.soinside.com 2019 - 2024. All rights reserved.