Page 1 of 1
Forum

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!



Share:
Notifications
Clear all

[Solved] Lazarus - Format Integer or Int64 with thousands separator

2 Posts
1 Users
0 Likes
4,126 Views
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

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-01]);
...

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.


   
ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 11 years ago
Posts: 1065
Topic starter  

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:='.';

   
ReplyQuote
Share: