Search code examples
inno-setuppascalscript

Inno Setup format a number with a thousand separator


I am trying to find a way to format a number with a thousand separator (in this case a comma), so that I can display the number in a more readable and friendly way to the user. I know that clearly I must first convert the integer to a string, using IntToStr and then probably use Length to return the length of the string and I have been looking at the possibility of using the Insert function to insert a comma. The problem is that Insert works from the left index position and I am struggling to work out how to do this from the right index. Additionally, I would have to add 1 to the string length for each insert and keep track of how many inserts that have been made. This seems quite a complicated way to do this, especially as the size of the number grows. Is there a simple way to format a number with a thousand separator that I am overlooking?


Solution

  • To format a number according to the current locale (rather than hard-coding US/UK-style "comma"), use the Format support function:

    var
      D: Integer;
      S: string;
    begin
      D := 1234567;
      S := Format('%.0n', [double(D)]);
    end;
    

    The S will be:

    • UK locale: 1,234,567
    • German locale: 1.234.567
    • Czech locale: 1 234 567

    The .0 specifies that you want no decimal points (as the input is an integer).

    For details, see https://docwiki.embarcadero.com/Libraries/en/System.SysUtils.Format