Using Format in a livebindings CustomFormat - delphi

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.

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

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.

imput 67.6666
CUSTOM FORMAT: ToStr(Format('%%.2f', Value)) + ' %%'
output 67.00 %

Related

How to format decimal places in Aspose.Word Template?

I want all numerical data to be formatted with 2 decimal places.
Is there a way to set this in the template word file (where I output the variable value via
<<[variableName] >>
), or even globally?
To format a numeric expression result, you can specify a format string as an element of the corresponding expression tag.
<<[variableName]:"0.##">>
See the following article for more information:
https://docs.aspose.com/words/net/outputting-expression-results/

Delphi - Formating a multiple %s string with one argument

When I try to format a string such as '%s%s' using a line of code like so:
format('%s%s', [x]);
I get an exception because you can't have multiple '%s' without using an array with the same amount of arguments such as:
format('%s%s', [x,x]);
However, I don't know how many '%s' I will have to format and therefore I don't know how long the array would have to be. I also only want '%s' assigned to only 1 value.
Is there a way in which you can use multiple '%s' and assign them all to the same index?
As described in the documentation you can use an index specifier to identify the argument by a zero based index. The index specifier is written immediately after the % and is followed by a :.
Your example would be:
Format('%0:s%0:s', [x])
MyStr := StringReplace('%s%s', '%s', x, [rfreplaceALL]);

Converting type string to long

I am trying to convert the type of string to long in the following code:
PaymentReceived = String.Format(new CultureInfo("en-IN", true), "{0:n}", t.PaymentReceived),
Here t.PaymentReceived is of type long, and the PaymentReceived is of type string but I want it to be of type long.
I am using this to convert the PaymentReceived value into comma separated value.
I am trying to do as of my knowledge like
PaymentReceived = Convert.ToInt64( String.Format(new CultureInfo("en-IN", true), "{0:n}", t.PaymentReceived))
But the error is Additional information: Input string was not in a correct format.
So please help me with another solution, thank you.
The formatter n, adds additional non-numeric characters. For en-IN culture, that means a number like 1000 ends up as 1,000.00.
The Convert.ToInt64 method requires that the string be 100% numeric, including no period, which might be fine for Convert.ToDecimal, but a long is not a float. Therefore, emphatically, your string is not formatted correctly, and the error is both obvious and correct. I'm not sure what your ultimate goal here is, but it makes no sense to convert a long to a formatted string and then immediately convert it back to a long, anyways.
Assuming you have only the string and you need to format it as a long, then you need to ensure that it's formatted as a long should be. That requires:
Split on the decimal point and take just the left side:
str = str.Split(new[] { '.' })[0];
Replace any commas with empty strings:
str = str.Replace(",", "");
That assumes you know the format will something like 1,000.00. Otherwise, you may want to use a regex to replace all non-numeric characters with an empty string, instead. However, you still need to split on the decimal. Otherwise, if you just removed all non-numeric characters from something like 1,000.00, then you'd end up with 100000, a number 100 times larger than the actual string number. Also, this is all dependent on the culture. Some cultures use , as the decimal separator and . and delimiter in large numbers. If you need to handle various cultures, you'll need to adjust accordingly.

Retrieve value by variable name in erlang function

Is it possible somehow to retrieve variable value by its name (name represented as string)?
% we are calling foo function as foo(3)
foo(Param) ->
Var1 = Param * 2,
% some magic code here which can evaluate
% "Var1" string to Var1's value (6)
ok.
I want to implement (if it is possible) some kind of logger macro, like
Param = 3*4,
% This should write "My parameter value is 12" to log
?LOG("My parameter value is $Param").
Thanks.
The common way to log is to have formatting string and list of parameters. However your idea is achievable through usage of parse transform.
Thanks to Dmitry Belyaev for mentioning parse transform.
Say we have logging code:
?dump("My parameter value is $Param")
What I need here is to parse variables within format string ("My parameter value is $Param") with some regular expression. This format string contains single var name (Param). And we need to insert io_lib:format function call (by transforming original AST) with modified format string:
print_message(io_lib:format("My parameter value is ~p~n", [Param]))
In result we can archive required behavior:
Bar = "hello",
Buzz = buzz123,
?dump("Say $Bar to $Buzz"),
% => example:19: Say "hello" to buzz123
You can look at my implementation here
For toy problems you could use:
io:format("My parameter value is ~p~n", [Param]).
See io_lib and io.
Alternatively:
error_logger:info_report/1
or other error_logger functions.
The logging library lager is commonly used.

Entering Text From Delphi To Word

I'm using Delphi XE2 and use the following code to enter the letter Y into a bookmark in a Word (2010) template.
Doc.Bookmarks.Item('NS').Range.InsertAfter('Y');
Except in the document, instead of the letter Y, the number 89 appears.
Is the fault likely to be from my code or in the Word document? Any direction gratefully received.
Your literal 'Y' is a character literal rather than a string string literal. The ASCII code for Y is 89.
So, you are passing a Char rather than a string. When Word needs to get a string representation of that integer it simply converts the integer 89 to its textual representation, the string '89'.
To get around the problem you can do this:
var
Text: string;
....
Text := 'Y';
Doc.Bookmarks.Item('NS').Range.InsertAfter(Text);
The idea is that we ensure that we pass a string to InsertAfter() rather than a character. Remember that InsertAfter() receives a variant parameter and so you do need to be careful about the type of the payload stored in the variant.

Resources