Displaying a value in a field rounded to only a few digits yet saving that value with more digits for more precise calculations

form example; precision; field; visible; instance data; Lotus Forms; designer; viewer; xfdl; compute; workaround to allow you to display fewer fraction digits in the input field than is saved in the instance field; avoiding the problem caused when a field has the float format round option set to round the entered number; When a field has the presentation of the float datatype format round option to display a rounded value; floating point calculations; formatting float data; floating point precision; formatting floating point precision in xfdl; When an xforms field formatted with datatype "float" and with presentation set to display a number of fraction digits

Technote (FAQ)

Question

This article provides a workaround to allow you to display fewer fraction digits in the input field than are saved in the instance field, avoiding the problem caused when a field has the float format round option set to round the entered number.

Cause

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.

Answer

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:

<amount1>12.4563</amount1>

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

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.

(Note: The &#xA;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.

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