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

时间:2013-03-26 20:34:02

标签: string delphi mask reportbuilder

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

就像这个例子:

50130301037855000150550010000000131000000132

我需要这个:

5013 0301 0378 5500 0150 5500 1000 0000 1310 0000 0132

有人有想法吗?感谢。

3 个答案:

答案 0 :(得分: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;

免责声明:此代码是在浏览器中编写的,未经过测试。如果您打算在对时间敏感的过程中使用它,我警告您可以优化此功能以获得更好的性能。

答案 1 :(得分:2)

我使用 DisplayFormat

找到答案
9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999;0; 

谢谢大家。

答案 2 :(得分: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;