How do I format an Integer using current locale in Delphi
Asked Answered
V

6

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

Vulpecula answered 17/11, 2008 at 16:7 Comment(0)
P
23

Try the format function.

Label1.Caption := Format('%.0n', [i + 0.0]);
Patric answered 17/11, 2008 at 16:24 Comment(0)
M
11

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

Mopup answered 17/11, 2008 at 20:57 Comment(0)
U
6

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

Unlike answered 17/11, 2008 at 16:23 Comment(0)
D
0

I have this function to do it, where d means perhaps decimal number:

function dn(i: integer): string; 
begin 
  result := format('%.0n', [i.ToDouble]) 
end;
Dandruff answered 14/5, 2020 at 2:40 Comment(0)
J
-1
stringreplace(format('%n',[1234567.0]),'.00','',[]);
Joby answered 17/11, 2008 at 16:36 Comment(0)
P
-1

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

Precipitate answered 17/11, 2008 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.