在实时绑定CustomFormat中使用格式 [英] Using Format in a livebindings CustomFormat
问题描述
我正在尝试使用LiveBindings格式化要在FireMonkey窗体上的TEdit中显示的数字。
I'm trying to use LiveBindings to format a number for display in a TEdit on a FireMonkey form.
我正在尝试在绑定的CustomFormat用来格式化带有两位小数的数字。
I'm trying to use the Format method in the CustomFormat of the binding to format the number with two decimal places.
我可以对输出进行硬编码:
I can 'hard code' the output:
Format("Hello", %s)
其中正在工作,但是我无法确定要使用哪种格式的字符串。如果我尝试使用标准格式字符串,例如
which is working, but I can't work out what formatting string to use. If I try a standard formatting string such as,
Format("%.2f", %s)
运行时错误格式无效或与参数不兼容。
I get a runtime error "Format invalid or incompatible with argument".
实际上,每当在格式字符串中包含%符号时,都会出现错误,因此我猜想Format接受了另一种类型的参数,但是找不到任何文档来说明正确的格式字符串是什么。 / p>
Indeed I get an error whenever I include a % symbol in the format string, so I'm guessing Format takes a different type of argument, but I can't find any documentation to say what the correct format string is.
推荐答案
参数作为%s传递到CustomFormat中。绑定系统会在将数据传递到评估程序之前准备好此参数。因此,CustomFormat字符串中的任何其他%符号都会产生错误。
The parameter is passed into CustomFormat as %s. The bindings system preparses out this parameter before the data is passed onto the evaluator. Thus any other % symbols in the CustomFormat string will give an error.
与普通格式字符串一样,您可以通过在字符串中加上一个双%(即% %)。
As with a normal format string you can include a literal % sign by putting a double % (i.e. %%).
因此,格式字符串中的任何%s都必须转换为%%,例如
So, any %s in the format string need to be converted to %%, e.g.
Format('%%.2f', %s)
解析为
Format('%.2f', 67.66666)
,然后解析为
67.67
用于显示。
如果要在最终输出中包含立即数%,您需要放入四进制%,例如
If you want to include a literal % in the final output you need to put a quadrupal %, e.g.
Format('%%.2f%%%%', %s)
成为
Format('%.2f%%', 67.6666)
并显示为
67.67%
注:普通格式函数使用最后一个参数h是一个值数组。绑定系统中的Format方法采用可变长度的参数列表。
Note: The normal format function takes a final parameter which is an array of values. The Format method in the bindings system takes a variable length list of parameters.
此外,方法名称区分大小写。 格式正确,格式将失败。
Also, the method names are case sensitive. 'Format' is correct, 'format' will fail.
这篇关于在实时绑定CustomFormat中使用格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!