如何使用Delphi中的当前语言环境格式化Integer

时间:2021-11-20 21:18:15
var i : integer;

i := 1234567;

Given the above, I want the string "1,234,567" as output (assuming UK locale). IntToStr just gives me "1234567". I'm sure there's a one-liner for this, but I can't find it...

鉴于上述情况,我希望字符串“1,234,567”作为输出(假设英国语言环境)。 IntToStr只给了我“1234567”。我确定这是一个单行,但我找不到它......

5 个解决方案

#1


17  

Try the format function.

尝试格式化功能。

Label1.Caption := Format('%.0n', [i + 0.0]);

#2


10  

Or if you need to be threadsafe or want to ensure you use the system default locale or want to specify one:

或者,如果您需要线程安全或者想要确保使用系统默认语言环境或想要指定一个:

function FormatIntFromLCID(const AValue: Integer; const LCID: Integer = LOCALE_SYSTEM_DEFAULT): string;
var
  AFormatSettings: TFormatSettings;
begin
  GetLocaleFormatSettings(LCID, AFormatSettings);
  Result := FormatFloat('#,##0',AValue, AFormatSettings);
end;

see this post for a more complete discussion about formatting/locales

有关格式化/区域设置的更完整讨论,请参阅此帖子

#3


6  

s := FormatFloat('#,##0', i);

s:= FormatFloat('#,## 0',i);

#4


-1  

Format('%n', [12345.678]);

#5


-1  

stringreplace(format('%n',[1234567.0]),'.00','',[]);

#1


17  

Try the format function.

尝试格式化功能。

Label1.Caption := Format('%.0n', [i + 0.0]);

#2


10  

Or if you need to be threadsafe or want to ensure you use the system default locale or want to specify one:

或者,如果您需要线程安全或者想要确保使用系统默认语言环境或想要指定一个:

function FormatIntFromLCID(const AValue: Integer; const LCID: Integer = LOCALE_SYSTEM_DEFAULT): string;
var
  AFormatSettings: TFormatSettings;
begin
  GetLocaleFormatSettings(LCID, AFormatSettings);
  Result := FormatFloat('#,##0',AValue, AFormatSettings);
end;

see this post for a more complete discussion about formatting/locales

有关格式化/区域设置的更完整讨论,请参阅此帖子

#3


6  

s := FormatFloat('#,##0', i);

s:= FormatFloat('#,## 0',i);

#4


-1  

Format('%n', [12345.678]);

#5


-1  

stringreplace(format('%n',[1234567.0]),'.00','',[]);