Formatting the float at ###. ## (two decimal places)

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 on any ###. ## (two decimal places). Using FloatToStrF is unsuccessful (it doesn't seem to work with DWORD).

thanks

+6
delphi
source share
2 answers

Just wondering if this is a math issue, not formatting. Why do you divide the number of items by 1000? Do you want to divide milliseconds (difference variable) by 1000? Perhaps 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 still want to format the result. You will need this as a property of a variable or class:

 MyFormatSettings: tformatsettings; 

then you will need to do this once, for example. in FormShow:

 getlocaleformatsettings(locale_system_default, MyFormatSettings); 

finally this should work:

 sb.panels[2].Text := format('%5.2f', EventRate, MyFormatSettings); 
+5
source share

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

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

Other functions:

 function FormatFloat(const Format: string; Value: Extended): string; overload; function FormatFloat(const Format: string; Value: Extended; const FormatSettings: TFormatSettings): string; overload; 
+9
source share

All Articles