将浮点数格式化为###。##(两位小数)

时间:2009-07-03 21:19:04

标签: delphi

拥有:

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)); 

我想将结果文本格式化为任何###.##(两位小数)。使用FloatToStrF并不成功(似乎无法使用DWORD)。

2 个答案:

答案 0 :(得分:9)

为什么不在format strings使用格式化功能?示例:

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

其他功能将是

function FormatFloat(const Format: string; Value: Extended): string; overload;
function FormatFloat(const Format: string; Value: Extended; const FormatSettings: TFormatSettings): string; overload; 

答案 1 :(得分:5)

只是想知道这是数学而不是格式化的问题。为什么要将项目数除以1000?你的意思是将毫秒(你的差异变量)除以1000吗?也许这就是你想要的:

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

当然,您仍然希望格式化结果。您需要将其作为变量或类属性:

MyFormatSettings: TFormatSettings;

然后,你需要做一次,例如在FormShow

getlocaleformatsettings(locale_system_default, MyFormatSettings);

最后,这应该有效:

sb.panels[2].Text := format('%5.2f', EventRate, MyFormatSettings);
相关问题