REAL to INT PLC Conversion: Rounding & Overflow
REAL-to-INT conversion changes a floating-point value into an integer value. It can round or truncate depending on the instruction and platform, and the result can overflow the target type. Choose the rounding rule explicitly, range-check before conversion and test negative x.5 values.
The REAL_TO_INT function converts a floating-point real value (REAL) to an integer value (INT). The fractional part is typically rounded to the nearest integer (banker's rounding in IEC 61131-3). Values outside the INT range (-32768 to 32767) may cause overflow. This conversion is used when calculation results need to be stored in integer registers, sent to analog outputs, or used as counter presets.
Parameters
Inputs
| Name | Type | Description |
|---|---|---|
| IN | REAL | Floating-point value to convert |
Outputs
| Name | Type | Description |
|---|---|---|
| OUT | INT | Rounded integer value |
How REAL-to-integer conversion works scan by scan
Read the waveforms and state table together. The diagram shows the sequence; the table states what the program should observe at each execution.
REAL-to-INT conversion decision path
The REAL input is first checked against the target range, then an explicit rounding rule produces the integer destination. Overflow or invalid floating-point inputs follow the documented error path.
State table
| Moment | Input | Internal state | Output | Engineering meaning |
|---|---|---|---|---|
| In range, below half | REAL = 20.4 | Nearest-integer conversion | INT = 20 | The fractional part is resolved by the selected nearest-integer rule. |
| In range, above half | REAL = 20.6 | Nearest-integer conversion | INT = 21 | A rounding conversion differs from truncation, which would produce 20. |
| Exact half | REAL = 2.5 or -2.5 | Tie-breaking rule applies | Platform/instruction dependent | Rockwell documents nearest-even behavior; never infer ties from one positive example. |
| Outside INT range | REAL > 32,767 or < -32,768 | Overflow / error path | Do not consume as a valid result | Range-check or convert to a wider integer and inspect the documented status. |
Ladder and Structured Text examples
Clamp an engineering value before conversion
|----[ ValueValid ]----[ LIM -32768.0 ScaledValue 32767.0 ]----|
| [ REAL_TO_INT ] |
| IN: ScaledValue |
| OUT: DisplayValue |The range check protects the destination; it does not define rounding. Select a named conversion, ROUND, TRUNC, CEIL or FLOOR according to the engineering requirement.
Explicit rounding with range and validity checks
ConversionValid := NOT IsInvalidReal
AND (ScaledValue >= -32768.0)
AND (ScaledValue <= 32767.0);
IF ConversionValid THEN
DisplayValue := REAL_TO_INT(ScaledValue);
ELSE
DisplayValue := 0;
ConversionFault := TRUE;
END_IF;Replace the invalid-REAL check with the mechanism supported by the controller. If truncation is required, use the platform’s explicit truncation instruction instead of relying on an implicit cast.
Vendor compatibility
A shared mnemonic does not guarantee an identical interface or state machine. Confirm the instruction help for the controller, firmware, language and library version used on the project.
| Platform | Instruction | What maps cleanly | Caveat to verify |
|---|---|---|---|
| Rockwell Logix | REAL-to-integer data conversion | Rounds to nearest; exact x.5 values round to the nearest even integer | Loss of data sets the overflow status flag; the stored target type still determines range. |
| Siemens STEP 7 V21 | REAL_TO_INT / CONVERT; ROUND, CEIL, FLOOR, TRUNC | REAL_TO_INT example rounds to nearest; separate instructions make other policies explicit | ENO is cleared for an out-of-range target or invalid floating-point value; verify CPU-specific behavior. |
| CODESYS / IEC-style runtimes | REAL_TO_INT and conversion functions | Explicit type conversion is available | Tie-breaking, overflow and invalid-value behavior must be read from the installed runtime/library documentation. |
Failure modes and edge cases to test
Positive and negative x.5 values
Test 1.5, 2.5, -1.5 and -2.5. A nearest-even rule does not always round halves away from zero.
REAL just beyond target range
The conversion must take the defined fault or clamp path; a wrapped value is not acceptable process data.
NaN or infinity enters the calculation
Treat the input as invalid and follow the platform’s status behavior instead of displaying a plausible integer.
Implicit conversion inside another instruction
The language may convert silently and set arithmetic status. Make the conversion explicit at the data boundary.
Decimal display needs fixed precision
Do not convert away required precision. Store scaled integer counts or retain REAL and format only at presentation.
Verification checklist
- 1Write down whether the requirement is nearest, truncate, floor or ceiling.
- 2Test positive, negative, exact-half and nearly-half inputs.
- 3Test target minimum, maximum and one value beyond each boundary.
- 4Verify controller status or ENO behavior for invalid and out-of-range inputs.
- 5Confirm that the chosen integer type preserves the required engineering range and resolution.
Current primary and technical sources
These sources support the behavior summarized here. The exact controller, firmware, instruction and installed-library help remain authoritative for implementation.
- REAL to Integer, Logix Designer
Rockwell Automation — Nearest-even examples and overflow-status behavior
- CONVERT: Convert value, STEP 7 V21
Siemens — Explicit conversion, rounding example and ENO error conditions
Platform-Specific Implementation
Siemens (TIA Portal)
Use CONVERT or REAL_TO_INT in TIA Portal. Uses round-half-even (banker's rounding).
Allen-Bradley (Studio 5000)
Use TRUNC for truncation or RND for rounding before integer conversion.
CODESYS
Use REAL_TO_INT() function. Rounds to nearest integer.
Common Applications
- Analog output writing
- Counter preset from calculation
- Display value formatting
- Register value storage
- Setpoint rounding
Common Mistakes to Avoid
- Not checking for overflow before conversion
- Unexpected rounding behavior (banker's rounding)
- Loss of precision for large values
Frequently Asked Questions
What is REAL_TO_INT (Real to Integer Conversion) in PLC programming?
Converts 32-bit floating-point value to 16-bit integer with rounding. The REAL_TO_INT function converts a floating-point real value (REAL) to an integer value (INT). The fractional part is typically rounded to the nearest integer (banker's rounding in IEC 61131-3).
What are common applications of REAL_TO_INT (Real to Integer Conversion)?
REAL_TO_INT (Real to Integer Conversion) is commonly used for: Analog output writing, Counter preset from calculation, Display value formatting, Register value storage, Setpoint rounding.
How do I use REAL_TO_INT (Real to Integer Conversion) in different PLC platforms?
Siemens: Use CONVERT or REAL_TO_INT in TIA Portal. Uses round-half-even (banker's rounding). Allen-Bradley: Use TRUNC for truncation or RND for rounding before integer conversion. CODESYS: Use REAL_TO_INT() function. Rounds to nearest integer.
Related Function Blocks
INT_TO_REAL (Integer to Real Conversion)
Converts 16-bit integer to 32-bit floating-point real value.
View Reference →BOOL_TO_INT (Boolean to Integer Conversion)
Converts FALSE to 0 and TRUE to 1 for numeric operations.
View Reference →INT_TO_STRING (Integer to String Conversion)
Converts integer value to its decimal string representation for display or logging.
View Reference →ANY_TO_ANY (Universal Type Conversion)
General type conversion framework covering all IEC 61131-3 type conversion functions.
View Reference →