> For the complete documentation index, see [llms.txt](https://help.qtech.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/floating-point-numbers.md).

# Floating-point numbers

Convert floating-point Modbus registers to integers for DATRAN VI, and back, in the DLP.

The XL4 Plus can read floating-point values from Modbus devices, such as water quality analysers, and convert them to integers for DATRAN VI. You do the conversion in the DLP with eight conversion commands.

{% hint style="info" %}
**Applies to:** XL4 Plus firmware V7.16 or later, with DLP IDE V4.04 or later. The RTU is the Modbus master.
{% endhint %}

## The commands

**Floating-point to integer**

| Byte order    | Precision        | Command            |
| ------------- | ---------------- | ------------------ |
| Big-endian    | 4 bytes (single) | `be_float_to_int`  |
| Little-endian | 4 bytes (single) | `le_float_to_int`  |
| Big-endian    | 8 bytes (double) | `be_double_to_int` |
| Little-endian | 8 bytes (double) | `le_double_to_int` |

**Integer to floating-point**

| Byte order    | Precision        | Command            |
| ------------- | ---------------- | ------------------ |
| Big-endian    | 4 bytes (single) | `be_int_to_float`  |
| Little-endian | 4 bytes (single) | `le_int_to_float`  |
| Big-endian    | 8 bytes (double) | `be_int_to_double` |
| Little-endian | 8 bytes (double) | `le_int_to_double` |

Each command takes six parameters: the input register, the output register, then the scaling.

```
command, input, output, input min, input max, output min, output max
```

Check your device's manual for its byte order. Big-endian means the most significant part of the number is in the first register and the least significant part in the next one. A single-precision value takes 4 bytes: two 16-bit registers.

## Example: a turbidity meter

This example reads turbidity from a Swan AMI Turbiwell meter, which stores floating-point values big-endian.

1. In QTech Workbench, set the RTU as a Modbus master and add an action to read the meter's input registers. See [Modbus](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/modbus.md).
2. Check in Workbench diagnostics that the RTU is reading the meter. In this example, analogue inputs 7 and 8 hold the turbidity value. Workbench shows each register as a decimal number, which isn't meaningful on its own.
3. Convert the value in the DLP:

```
proginit
progstart
; example 1, with the integer value as float value x 100
be_float_to_int rain7, nain1, 0.0, 300.0, 0, 30000
; example 2, with the integer value using the full range
be_float_to_int rain7, nain2, 0.0, 300.0, 0, 65535
progend
```

### Which scaling to use

* **Example 1** gives an integer that's 100 times the real value, so it's easy to read in Workbench: 550 means 5.5 NTU. To get the real value back, divide by 100. It loses some precision.
* **Example 2** uses the integer's full range, so it loses the least precision. To get the real value back in DATRAN VI, use a formula with the `scale` function.

In this example, the full-range value (example 2) was within 0.01% of the true value. DATRAN VI can also work out the true value directly from the two input registers with its `float` function.
