Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] Lazarus - Format Integer or Int64 with thousands separator
Delphi, Lazarus, Free Pascal
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter
March 25, 2019 6:44 AM
This took me a few seconds to figure out, so as usual I post my "solution" here.
What is the situation?
I have a file size (using the FileUtils.FileSize('filename') function in bytes (Int64).
However I'd like to display that with the thousands separator, so for example 1234567890 becomes 1,234,567,890.
the default functions didn't like the fact that I passed it an Integer (why would this even be a problem?) and the documentation of the Format function (here and here) are kind-a crappy ... (I can't complain, since I'm not updating the Wiki page either)
Anyhoo, the fix:
var
FileSizeInBytes:int64;
...
Label1.Caption := Format('%.0N',[FileSizeInBytes+0.1-0.1]);
...
To make the Integer (int64) a float, I add 0.1 and next subtract 0.1. To format the new "float" without numbers behind the decimal point (.0) and have it use thousands separators (N), use the "%.0N" format placeholder.
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter
February 3, 2021 10:34 AM
Note:
If the thousands separator does not appear, then your'll need to set if the DefaultFormatSettings.DecimalSeparator value.
I've done it like this (since I didn't find a reliable other method):
if DefaultFormatSettings.DecimalSeparator='.' then
DefaultFormatSettings.ThousandSeparator:=','
else
DefaultFormatSettings.ThousandSeparator:='.';