将float格式化为###。##(两位小数) [英] Formatting a float to ###.## (two decimals)

查看:310
本文介绍了将float格式化为###。##(两位小数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var 
差异:DWORD //差异以毫秒为单位显示
//列表.Items.Count可以是0到########
[...]
sb.panels [2] .Text:= FloatToStr((((List.Items.Count )/差异)/ 1000));

我想将生成的文本格式化为任何###。##(两位小数)。使用FloatToStrF是没有成功的(似乎不适用于DWORD)。



谢谢

解决方案

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

  EventRate:=(List.Items.Count)/(差异/ 1000); //每秒事件;每分钟需要更改1000到60000 

当然,你还是要格式化结果。您需要将其作为变量或类属性:

  MyFormatSettings:tformatsettings; 

那么你需要这样做一次,例如在FormShow中:

  getlocaleformatsettings(locale_system_default,MyFormatSettings); 

最后,这应该工作:

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


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

解决方案

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

这篇关于将float格式化为###。##(两位小数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆