Format Customformat value in Livebinding

Asked

Viewed 1,135 times

5

I’m having trouble trying to use the property CustomFormat of Livebinding, I am trying to format a value obtained from database to number format with thousand separator.

I’m using:

Format('%d',[Self.Owner.FieldByName('soma').Text])   

However the following error is returned:

Invalid or incompatible with argument

2 answers

2

You’re passing a String to be formatted for numeric is that right? It wouldn’t be like this:

Format('%d',[Self.Owner.FieldByName('soma').AsInteger])
  • I’ll test and report.

  • Claudio did not work, I will give more details the value is returned from a datasnap server using the technique of Reflection using JSON the value in the SQL database is a decimal (similar to money). I receive the data in a memtable and do Binding in a listview.

  • Sorry for the delay in answering, if it is a decimal try so : Format('%10.2f',[Self.Owner.Fieldbyname('soma'). Asfloat])

2

If you are passing a value with floating point, do not use format %d, it is used to whole, use %m for monetary values or %n for floating point values. Your code can look like this:

Format('%n', [Self.Owner.FieldByName('soma').AsFloat]); 

Updating

According to the page Default Livebindings Methods the functioning of the function Format is a little different when using Livebindings, instead of specifying a array of arguments [.., ...], is added as a parameter.

For example, the normal use of SysUtils.Format it’s usually like this:

Format('%d %d', [1, 2])

In Livebindings one should use:

Format('%d %d', 1, 2)

Following this reasoning you can use:

Format('%n', Self.Owner.FieldByName('soma').AsFloat);  
  • I did the test does not work the app simply closes, I debug and found that the error is the same that I reported at the beginning of the post, Find Invalid or incompatible with argument. I believe that the format syntax when used in livebinding should be another.

  • I’ve tried everything in the way There’s always the same mistake.

  • @edmjunio Will that value Self.Owner.FieldByName('soma') is not of another kind? a string maybe? try changing the format of %n for %s and .AsFloat for .Text or .AsString (I don’t know if it exists).

  • I tried everything that way there is even a call about it here on the site, I did tests based on it but I could not. http://stackoverflow.com/questions/18801484/using-format-in-a-livebindings-customformat

  • 1

    If you have opportunity could try to reproduce the error, just use a Prototypebindsource component and a listview. Bind Include in Prototypebindsource an item of the currency type and in the customformat property try to format with decimal and thousand separator, in case the %n.

  • @edmjunio I’ve been reading about it and it really feels like %n will have no effect and %s is reserved. I was able to reproduce the error. So on the property CustomFormat used Format('%%.2f', %s) and UpperCase(%s) both did not launch error. Take a look at this link, there talks about these expressions in Binding. That one article can be very useful too.

  • I had already read these two articles. The first was what I was basing up to now. The second I didn’t want to have to reach this level of having to create a class register the methods etc etc. But I don’t think there will be a way out.

  • Complicated. A TFloatField class TNumericField can solve this problem using the property DisplayFormat format #.00. :)

  • I get the value via json datasnap populating a memtable Binding is not done directly in the field

  • @edmjunio In this case I have nothing in mind of what can be done. Perhaps the content of the second link can be a plausible alternative. =)

  • 1

    I will implement tomorrow the recommendation of the second link and report

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.