How can you set a floating point input field to display just two decimal digits, such as with currency, while your instance data saves four decimal digits for more precise calculations?
When you have an XForms field formatted with a datatype "float" and the presentation is set to display a number of digits, the numerical data you enter is formatted with the presentation options before it is written to the instance. To state that another way, when a field has the presentation of the float datatype format round option to display a rounded value, any entered value will be rounded as necessary before being placed in the instance.
Thus, the instance data cannot hold more precision than is displayed by the field that it references. Thus, no matter how many decimal points of precision the user enters, the precision of the instance is set by the float format rounding option on the field in which the data was entered.
The example below shows you how to code a form so that it saves more fractional digits in the instance than are displayed by the field. The result is two co-located fields, one for display and one for data entry and instance reference.
When you click a field for the first time and enter a value, you see the full precision, as shown:
When you tab out of the field, that first field becomes invisible, and the other co-located field appears in its place, displaying the rounded value, as shown:
If you click back to that field, the full-precision field becomes visible and available for data entry, while the read-only field becomes invisible again. The instance data stores the full precision, as follows:
Steps to code the form:
1. Create the instance
In the Forms Designer, create an instance to hold the value you are going to save or use in calculations. In this example, we use <amount1>.
Create another instance to hold the display value. In this example, we use <display1>.
If you need help creating an instance, refer to the Lotus Forms Information Center.
Your instance view will look something like this screen capture:
Your code within the xforms:model will look something like this:
<xforms:instance id="INSTANCE" xmlns="">
2: Add the fields to the form, and set their properties
In the Design view, add two xforms:input fields, FIELD1 and FIELD2.
Select FIELD1, and in the Properties view, expand Format, and format.
Set the datatype to float.
Expand the presentation section, and set fractiondigits to 4.
For FIELD2, do the same but set fractiondigits to 2, as shown in this screen capture:
Set FIELD2, the display field, to be readonly, as shown:
3. Bind the fields to the instance
Drag the related instance onto each field to bind the fields to the instance.
Screen capture of dragging the fields:
4. Add a compute to FIELD1
Next, you need to change the functioning of FIELD1's visibility.
In the Source tab, in FIELD1, add a compute inside the <visible> tag that makes it so that when you tab out of FIELD1, which toggles the FIELD1's "focused" from 'on' to 'off', FIELD1 becomes invisible.
If FIELD2 has focus, that is, if you click FIELD2, then FIELD1 becomes visible again. In the default case, the value inside the tag remains the same.
Your compute should look like this:
toggle(focused, 'on', 'off') == '1'
: (toggle(FIELD2.focused, 'off', 'on') == '1'
at the end of each line causes the Source view to maintain newlines within your XFDL code statements. Otherwise, this compute would turn into one long line, making it hard to read).
5. Add the computes to FIELD2
Next, you need to change the functioning of FIELD2. In the Source tab, in FIELD2, add a custom compute that takes the focus from FIELD2 and gives it to FIELD1. That way, when you click FIELD2, FIELD 1 will automatically get the focus.
The code should look like this:
toggle(focused) == '1'
? set('FIELD1.focused', 'on')
As well, you need to add one more compute inside the <visible> tag for FIELD2 that parallels the one inside FIELD1's <visible> tag. When FIELD2 no longer has focus, as done by the custom compute above, FIELD2 becomes invisible. In the default case, value inside the <visible> tag remains the same
The code inside the <visible> tag will look like this:
toggle(focused, 'on', 'off') == '1'
: (toggle(FIELD1.focused, 'on', 'off') == '1'
6. Set the layout
Finally, in the Design view, you need to lay one field exactly on top of the other. You can use the Properties view to check if the locations and sizes are exactly the same.
Screen capture of Properties view:
Note: This solution is not advisable for use with the Webform Server, which does not register Focused events unless modified from the default configuration.