Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format an Integer using current locale in Delphi

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...

like image 439
Roddy Avatar asked Nov 17 '08 16:11

Roddy


Video Answer


2 Answers

Try the format function.

Label1.Caption := Format('%.0n', [i + 0.0]);
like image 85
Bruce McGee Avatar answered Oct 13 '22 23:10

Bruce McGee


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

like image 30
Francesca Avatar answered Oct 13 '22 22:10

Francesca