我正在尝试使用 LiveBindings 来格式化数字,以便在 FireMonkey 表单上的 TEdit 中显示。
我正在尝试使用绑定(bind)的 CustomFormat 中的 Format 方法来格式化具有两位小数的数字。
我可以对输出进行“硬编码”:
Format("Hello", %s)
它正在工作,但我无法弄清楚要使用什么格式字符串。如果我尝试使用标准格式字符串,例如,
Format("%.2f", %s)
我收到运行时错误“格式无效或与参数不兼容”。
事实上,每当我在格式字符串中包含 % 符号时,我都会收到错误,所以我猜测 Format 采用不同类型的参数,但我找不到任何文档来说明正确的格式字符串是什么。
最佳答案
您不能在 LiveBindings -> CustomFormat
中使用 Format('%.2f',[%s])
%s 是为数据保留的,对于 TEdit 来说,它是一个字符串
d : double;
s : string;
...
d := 1234.5678;
s:=Format('%.2f',[d]);
Format() 是将 [int、decimal、double、float] 转换为 字符串 。
所有其他都会给你一个错误:无效参数
例如,有效
TLinkControlToField1 -> CustomFormat : "Double : "+UpperCase(%s)
将在Edit1.text中给你
Double : 1234.5678
好的,我们知道“1234.5678”的 Uppercase() 没有任何效果。
只是为了显示(%s)是一个字符串
解决方案:
- 设置为 TFloatField -> DisplayFormat
#00000.00
回合并显示01234.57
- 检查 TFloatField -> 货币
回合并显示1234.57
- 使用组件看这里
LiveBindings in XE3: Formatting your Fields
关于delphi - 在 livebindings CustomFormat 中使用 Format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801484/