Using Format in a livebindings CustomFormat

我们两清 提交于 2019-12-24 13:03:39

问题


I'm trying to use LiveBindings to format a number for display in a TEdit on a FireMonkey form.

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".

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.


回答1:


You can not use Format('%.2f',[%s]) in LiveBindings -> CustomFormat

The %s is reserved for the data and for a TEdit , it's a string

d : double;
s : string;
...
d := 1234.5678;
s:=Format('%.2f',[d]);

Format() is to convert [int, decimal, double, float] to a string .
all other give you a error : invalid argument
valid is for example

TLinkControlToField1 -> CustomFormat : "Double : "+UpperCase(%s)

will give you in Edit1.text

Double : 1234.5678

OK , we know that Uppercase() for '1234.5678' has no effects .
Is only to show (%s) is a string

Solutions:

  • Set to TFloatField -> DisplayFormat #00000.00
    rounds and display 01234.57
  • check TFloatField -> currency
    rounds and display 1234.57
  • use a component look here
    LiveBindings in XE3: Formatting your Fields




回答2:


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. %%).

So, any %s in the format string need to be converted to %%, e.g.

Format('%%.2f', %s)

which gets parsed out to

Format('%.2f', 67.66666)

and then parsed down to

67.67

for display.

If you want to include a literal % in the final output you need to put a quadrupal %, e.g.

Format('%%.2f%%%%', %s)

becomes

Format('%.2f%%', 67.6666)

and displays as

67.67%

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.



来源:https://stackoverflow.com/questions/18801484/using-format-in-a-livebindings-customformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!