3

This question is relaetd to This question, on using some of the windows explorer features automatically inside a Delphi application.

Is there a way to format an integer using the metrix prefixes automatically in Delphi? Somehow to automatically obtain a result like windows explorer gives? I mean converting 1024 to 1.0 K automatically.

let's say something like

FormatMetric('FileSize = %d', [26112], 1,'B')
// where the third parameter is the number of decimal digits
// and the fourth is the string that is appended

will return

25.5 KB

Of course I can code this, but is there in the RTL something like this?

Community
  • 1
  • 1
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249

1 Answers1

6

You need the Windows API call StrFormatByteSizeA.

See the msdn: http://msdn.microsoft.com/en-us/library/bb759974%28VS.85%29.aspx

The_Fox
  • 6,992
  • 2
  • 43
  • 69
  • Thanks, this API does exactly what I need. I can imagine there is no Delphi wrapper for this, but it is not needed. – UnDiUdin May 26 '10 at 12:32
  • There is a Delphi declaration for both `StrFormatByteSizeA` and `StrFormatByteSizeW` in `ShLwAPI.pas; just add it to your uses clause as usual. – Ken White May 26 '10 at 12:55
  • Yes, I discovered this just now. Anyway even with StrFormatByteSizeW I cannot get values > than 2GB. WHy this? – UnDiUdin May 26 '10 at 12:57
  • @Ken White: That unit does not exist in Delphi 7, so I didn't know that. – The_Fox May 26 '10 at 13:00
  • @user193655: StrFormatByteSizeW works fine for values > 2GB. Why doesn't it work for you? Do you get an error? – The_Fox May 26 '10 at 13:21
  • My fault, I made a test application with a TSpinEdit, whose Value was cast to a Int and not to Int64, now I tried with a simple TEdit and StrToInt64 and I was able to obtain good results for > 2GB. – UnDiUdin May 26 '10 at 15:55
  • @The_Fox: There was no mention of the Delphi version in the original question. If you're not using a recent version, you should let people know that when you post, so they can adjust their answers accordingly. – Ken White Jun 01 '10 at 14:14