How to format an integer using the current locale in Delphi

var i : integer;

i := 1234567;

Given the above, I want the string "1,234,567" to be output (assuming that in the British language). IntToStr just gives me "1234567". I am sure that there is one liner for this, but I can not find it ...

+3
source share
5 answers

Try the format function.

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

Or if you need to be thread safe or want you to use the default locale or want to specify it:

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 of formatting / locales

+10
source

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

+6
stringreplace(format('%n',[1234567.0]),'.00','',[]);
-1

('% n', [12345.678]);

-1
source

Source: https://habr.com/ru/post/1699668/


All Articles