views:

105

answers:

1

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?

+6  A: 

You need the Windows API call StrFormatByteSizeA.

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

The_Fox
Thanks, this API does exactly what I need. I can imagine there is no Delphi wrapper for this, but it is not needed.
There is a Delphi declaration for both `StrFormatByteSizeA` and `StrFormatByteSizeW` in `ShLwAPI.pas; just add it to your uses clause as usual.
Ken White
Yes, I discovered this just now. Anyway even with StrFormatByteSizeW I cannot get values > than 2GB. WHy this?
@Ken White: That unit does not exist in Delphi 7, so I didn't know that.
The_Fox
@user193655: StrFormatByteSizeW works fine for values > 2GB. Why doesn't it work for you? Do you get an error?
The_Fox
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.
@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