How do I format an Integer using current locale in Delphi

天涯浪子 提交于 2019-12-18 17:00:43

问题


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:


Try the format function.

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



回答2:


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:


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




回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/295992/how-do-i-format-an-integer-using-current-locale-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!