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?
Asked
Active
Viewed 284 times
0

Martin Prikryl
- 188,800
- 56
- 490
- 992

Robert Wigley
- 1,917
- 22
- 42
1 Answers
2
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

Martin Prikryl
- 188,800
- 56
- 490
- 992
-
Brilliant! Martin you are a star! That's the simple (and better than hard-coding) solution I was looking for, but couldn't find. – Robert Wigley Sep 27 '15 at 19:46