2009-07-03 64 views
6

有:。格式化浮子###。##(保留兩位小數)

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

感謝

回答

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

我已經嘗試所有這些解決方案..例如: sb.panels [2]。文本:= FormatFloat('#,# #(0),((List.Items.Count)/ difference)/ 1000); 結果總是0.那麼超載版本呢?你有一個使用它的例子嗎? – volvox 2009-07-04 02:09:15

+0

也許在你的本地人中,意思是1k的分隔符,你應該使用。代替。 – 2009-07-04 06:57:18