2

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.

Mike Sutton
  • 4,191
  • 4
  • 29
  • 42
  • There is documentation for the CustomFormat property in http://docwiki.embarcadero.com/RADStudio/Sydney/en/Default_LiveBindings_Methods, however, the example they give Format("%d %d", 1, 2) gives the same invalid or incompatible with argument error. – SiBrit Jun 09 '21 at 22:46

3 Answers3

3

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:


enter image description here

moskito-x
  • 11,832
  • 5
  • 47
  • 60
3

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.

Mike Sutton
  • 4,191
  • 4
  • 29
  • 42
0

imput 67.6666

CUSTOM FORMAT: ToStr(Format('%%.2f', Value)) + ' %%'

output 67.00 %