# Number

## What it does

The number component is designed to enter integer or decimal numbers. See [Currency](https://doc.orbeon.com/form-runner/component/currency) for a similar component designed to enter currency amounts.

![Number field with unit](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEkBiSDvlXs3VWiO0Zu%2F-LEkBmMpnaxnftLGDglW%2F-LEkC3_u7rmYB2bPWzDl%2Fxbl-number.png?generation=1528743361578803\&alt=media)

## Basic usage

You use the number component like a regular input field, for example:

```xml
<fr:number ref="my-number">
  <xf:label>Quantity</xf:label>
</fr:number>
```

### Form builder support

Form builder shows extended control settings for Number and Currency fields:

![Number and Currency field control settings](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-54bb537ae28a0f4b480af66f2346ed5c1ceb9896%2Fxbl-number-control-settings.png?alt=media)

These are also available at the form level:

![Number and Currency field form settings](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-ca8f3c32ef3311b5e9a0647ff044e76ed6795087%2Fxbl-number-form-settings.png?alt=media)

## Datatype

`fr:number` must be bound to either of:

* `xs:decimal`
* `xs:integer` \[SINCE Orbeon Forms 2016.1]

## Parameters

`fr:number` supports parameters, which you can set via properties or directly on `fr:number`:

* `prefix`: optional prefix shown before the number
* `suffix`: optional suffix shown after the number
* `digits-after-decimal`: digits to show after the decimal point (by default 0, which means the number is an integer)
* `decimal-separator`: single character to use as decimal separator
  * use `.` or `,` only
* `grouping-separator`: single character to use as thousands separator separator (can be blank)
* `round-when-formatting`: when formatting the number for display, whether to round the value to `digits-after-decimal` if there are more digits after the decimal point or not. The default is `false`.
  * SINCE Orbeon Forms 2016.1
  * UNTIL Orbeon Forms 2017.2: Rounding uses the [half to even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) method.
  * SINCE Orbeon Forms 2018.1: Rounding uses the [half up](https://en.wikipedia.org/wiki/Rounding#Round_half_up) method.
* `round-when-storing`: when storing the number entered by the user, whether to round the value to `digits-after-decimal` if there are more digits after the decimal point or not. The default is `false`.
  * SINCE Orbeon Forms 2016.1
  * UNTIL Orbeon Forms 2017.2: Rounding uses the [half to even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) method.
  * SINCE Orbeon Forms 2018.1: Rounding uses the [half up](https://en.wikipedia.org/wiki/Rounding#Round_half_up) method.
* `pattern`
  * SINCE Orbeon Forms 2016.1
  * overrides the HTML `pattern` attribute
* `field-align`: determines the alignment of the value in the field
  * SINCE Orbeon Forms 2022.1.1
  * `left` (default): align content to the left
  * `right`: align content to the right

These are the default values of the properties:

```xml
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.prefix"
    value=""/>
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.suffix"
    value=""/>
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.digits-after-decimal"
    value=""/>
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.decimal-separator"
    value="."/>
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.grouping-separator"
    value=","/>
<property
    as="xs:boolean"
    name="oxf.xforms.xbl.fr.number.round-when-formatting"
    value="false"/>
<property
    as="xs:boolean"
    name="oxf.xforms.xbl.fr.number.round-when-storing"
    value="false"/>
```

## Validation vs. formatting

The `digits-after-decimal` parameter controls how the value entered by the user gets *formatted* when the field displays it. This does not impact validation of the data at all.

\[SINCE Orbeon Forms 2016.1]

In order to validate the number to have a specific number of decimal digits, the `xxf:fraction-digits()` function should be used.

## Mobile support

\[SINCE Orbeon Forms 2016.1]

On iOS, when the control identifies that the value is a non-negative integer, it shows a numeric keypad:

![iOS numeric keypad](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEkBiSDvlXs3VWiO0Zu%2F-LEkBmMpnaxnftLGDglW%2F-LEkC3_ogtin2n56SS4v%2Fxbl-number-numeric-keypad.png?generation=1528743351335920\&alt=media)

This is the case if:

* the control is bound to `xs:integer` OR is bound to `xs:decimal` and has an [`xxf:fraction-digits(0)`](https://doc.orbeon.com/contributors/extension-validation#xxffraction-digits) constraint
* AND has an [`xxf:non-negative()`](https://doc.orbeon.com/contributors/extension-validation#xxfnon-negative) constraint

Otherwise, the control shows the numeric pane of the regular keyboard:

![iOS numeric keypad](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEkBiSDvlXs3VWiO0Zu%2F-LEkBmMpnaxnftLGDglW%2F-LEkC3_sbiVNTVU7AljZ%2Fxbl-number-numeric-pane.png?generation=1528743351359729\&alt=media)

## Examples

### Use of a suffix

Here is an example with a suffix:

![Number field with suffix](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LEkBiSDvlXs3VWiO0Zu%2F-LEkBmMpnaxnftLGDglW%2F-LEkC3_u7rmYB2bPWzDl%2Fxbl-number.png?generation=1528743361578803\&alt=media)

```xml
<fr:number ref="my-number" suffix="m/s">
    <xf:label>Number</xf:label>
    <xf:hint>Number field with validation</xf:hint>
</fr:number>
```

### Rounding

\[SINCE Orbeon Forms 2016.1]

#### Rouding when formatting only

Assume the following properties or corresponding attributes:

```xml
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.digits-after-decimal"
    value="2"/>
<property
    as="xs:boolean"
    name="oxf.xforms.xbl.fr.number.round-when-formatting"
    value="true"/>
<property
    as="xs:boolean"
    name="oxf.xforms.xbl.fr.number.round-when-storing"
    value="false"/>
```

Scenario:

* User types "12345.678".
* Because `round-when-storing` is `false`, the value is not rounded and stored as is in the data.
* When the user focuses out of the field, the value is formatted and rounded, because `round-when-formatting` is set to `true`.
* Field displays "12,345.68".

This is useful if you want to keep the full precision of decimal values in the data, but format them nicely to a given number of decimals.

#### Rounding when formatting and when storing

Assume the following properties or corresponding attributes:

```xml
<property
    as="xs:string"
    name="oxf.xforms.xbl.fr.number.digits-after-decimal"
    value="2"/>
<property
    as="xs:boolean"
    name="oxf.xforms.xbl.fr.number.round-when-formatting"
    value="true"/>
<property
    as="xs:boolean"
    name="oxf.xforms.xbl.fr.number.round-when-storing"
    value="true"/>
```

Scenario:

* User types "12345.678".
* Because `round-when-storing` is `true`, the value is rounded and stored as "12345.68" in the data.
* When the user focuses out of the field, the value is formatted and rounded, because `round-when-formatting` is set to `true`.
* Field displays "12,345.68".

*NOTE: Here rounding during formatting doesn't change the value since it was rounded when stored in the first place.*

This is useful if you don't want to keep the full precision of decimal values in the data and want to force a maximum number of digits after the decimal point.

### Right-aligned content

\[SINCE Orbeon Forms 2022.1.1]

Regular controls:

![Right-aligned content](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-1ef3ab63c296aef0f2c41499aa1125f69b6f113e%2Fxbl-number-right-aligned.png?alt=media)

Within a repeated grid:

![Right-aligned content in a repeated grid](https://4129616727-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LEkBiSDvlXs3VWiO0Zu%2Fuploads%2Fgit-blob-9c54f29ddc88f289641190bcbecfe32a6318d38d%2Fxbl-number-right-aligned-repeated-grid.png?alt=media)

## See also

* Blog post: [Better numeric input on mobile](https://blog.orbeon.com/2016/01/better-numeric-input-on-mobile.html)
* Blog post: [Right-aligning number and currency fields](https://blog.orbeon.com/2023/08/right-aligning-number-and-currency.html)
