How do I format an Integer using current locale in Delphi

后端 未结 6 1599
长发绾君心
长发绾君心 2021-01-03 02:56
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\"

相关标签:
6条回答
  • 2021-01-03 03:27

    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;
    
    0 讨论(0)
  • 2021-01-03 03:39

    Try the format function.

    Label1.Caption := Format('%.0n', [i + 0.0]);
    
    0 讨论(0)
  • 2021-01-03 03:46

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

    0 讨论(0)
  • 2021-01-03 03:47
    stringreplace(format('%n',[1234567.0]),'.00','',[]);
    
    0 讨论(0)
  • 2021-01-03 03:48

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

    0 讨论(0)
  • 2021-01-03 03:49

    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

    0 讨论(0)
提交回复
热议问题