tags:

views:

283

answers:

3

Having :

var
Difference: DWORD // difference shows in milliseconds
// List.Items.Count can be any 0 to ######## 
[...]
sb.panels[2].Text  := FloatToStr((((List.Items.Count) / difference) / 1000));

I want to format the resulting text to any ###.## (two decimals). Using FloatToStrF is no success (does'nt seem to work with DWORD).

Thanks

A: 

see this link

Adinochestva
+6  A: 

Why don't you use format function with format strings? Example:

sb.panels[2].Text := Format('%8.2f',[123.456]);

Other functions would be

function FormatFloat(const Format: string; Value: Extended): string; overload;
function FormatFloat(const Format: string; Value: Extended; const FormatSettings: TFormatSettings): string; overload;
Ralph Rickenbach
I have already try all those solutions .. example:sb.panels[2].Text := FormatFloat('#,##0',((List.Items.Count) / difference) / 1000); Result is always 0. What about the overload version? You have an example using it?
volvox
Maybe in your locals the , means the 1k delimiter and you should use . instead.
Ralph Rickenbach
Thank you for the info, appreciated. My problem is now resolved.
volvox
+2  A: 

Just wondering if this is a problem with math rather than formatting. Why are you dividing the number of items by 1000? Do you mean to divide milliseconds (your Difference variable) by 1000? Maybe this is what you want:

EventRate := (List.Items.Count) / (difference / 1000);  // events per second; to make it per minute, need to change 1000 to 60000

Of course, you'll still want to format the result. You'll need this as a variable or class property:

MyFormatSettings: tformatsettings;

then, you'll need to do this once, e.g. in FormShow:

getlocaleformatsettings(locale_system_default, MyFormatSettings);

finally, this should work:

sb.panels[2].Text := format('%5.2f', EventRate, MyFormatSettings);
Argalatyr
If the OP wants to show the time necessary to process one list item, shouldn't it be something like: "SomeFloat := 0.001 * (difference / List.Items.Count);" instead? Of course 0 items need to be handled before...
mghie
Yes i do have to divide.EventPerMinute := List.Items.Count / difference / 1000;Some solutions:sb.panels[2].Text := floattostr(eventperminute); // OK but not formattedsb.panels[2].Text := floattostrF(EventPerMinute ,ffNumber,4,3); // always 0sb.panels[2].Text := Format('%f',[eventperminute]); // always 0sb.panels[2].Text := FormatFloat('#,##0',EventPerMinute); // always 0---------------This calculation is on a Timer event, triggered every 1000 ms.
volvox
if there are 10 list items in 5000 milliseconds, then 10 / 5000 / 1000 will yield 0.000002 - no wonder you get zero! Even if you do as I suggest, 10 / (5000 / 1000) = 2 this is in events PER SECOND not per minute. For per minute, you'd have to divide by 60000!
Argalatyr
I've edited variable name and added a source comment line to reflect my previous comment
Argalatyr
Ah .... my fault. You are correct, i should have divided by 60000. Now even ###.## is showing. Thanks for the MyFormatting class prop. hint.
volvox