Control Rexx Decimal Precision and Restore Numeric State
Control Rexx decimal arithmetic precision and notation with NUMERIC DIGITS and NUMERIC FORM, then restore the previous state.
07 May 2026, 01:16 UTC

Problem and useful takeaway
Rexx performs decimal arithmetic by default, but the interpreter rounds results to the current NUMERIC DIGITS setting. If a script changes precision for one calculation and does not restore it, later unrelated calculations inherit the new precision. The practical fix is to save DIGITS() and FORM(), set the precision and notation you need, run the calculation, then restore the previous values before the routine ends.
Desired outcome
A Rexx script performs financial-style decimal calculations at a chosen precision, displays results in SCIENTIFIC or ENGINEERING notation when appropriate, and leaves the interpreter's numeric state unchanged for code that runs afterward.
Prerequisites
- A Rexx interpreter installed and available on the command path, such as IBM Object Rexx, Open Object Rexx, or Regina Rexx.
- A text editor to create a script file, for example
calc.rexx. - No administrative permissions are required to run a user-owned Rexx script.
Confirm the interpreter's default digit count before relying on nine digits; some embedded Rexx variants use a different default.
How Rexx controls decimal precision
Rexx uses variable-length decimal numbers rather than binary floating point. NUMERIC DIGITS n sets the number of significant digits used for addition, subtraction, multiplication, division, and power operations. The interpreter keeps internal guard digits during a calculation and rounds the final result to the current digit setting.
NUMERIC FORM SCIENTIFIC or NUMERIC FORM ENGINEERING selects exponential notation when a result crosses the digit threshold. The built-in functions DIGITS() and FORM() return the current precision and notation setting.
Procedure: set precision, calculate, restore
1. Query and save the current setting
Before changing anything, capture the values that are active now.
saved_digits = DIGITS()
saved_form = FORM()
2. Set the precision and notation form
Choose a digit count large enough for the calculation. A value such as 20 is often sufficient for financial-style work; larger values increase memory and CPU use.
NUMERIC DIGITS 20
NUMERIC FORM ENGINEERING
Use SCIENTIFIC for general scientific exponential notation; use ENGINEERING when exponents should align to multiples of three, matching units such as kilo, mega, and milli.
3. Run the calculation
say '1 / 3 =' 1/3
say '0.1 + 0.2 =' 0.1 + 0.2
4. Restore the previous setting
Because NUMERIC DIGITS persists for the routine or session, restore the saved values before the script exits.
NUMERIC DIGITS saved_digits
NUMERIC FORM saved_form
Complete example script
Run this file from a shell or command prompt where the Rexx interpreter is available. No special permissions are needed.
/* numeric_control.rexx */
/* Save current numeric state */
saved_digits = DIGITS()
saved_form = FORM()
/* Apply calculation precision */
NUMERIC DIGITS 20
NUMERIC FORM ENGINEERING
say 'Division with 20 digits:'
say '1 / 3 =' 1/3
say 'Decimal addition:'
say '0.1 + 0.2 =' 0.1 + 0.2
/* Integer division and remainder avoid rounding on division */
say 'Integer quotient:' 10 % 3
say 'Remainder:' 10 // 3
/* Restore state for the rest of the session */
NUMERIC DIGITS saved_digits
NUMERIC FORM saved_form
say 'Restored digits:' DIGITS()
say 'Restored form:' FORM()
Where to run and expected checks
- Run from a terminal:
rexx numeric_control.rexxorregina numeric_control.rexx, substituting your interpreter name. - Expected check: the division output shows approximately twenty significant digits, while
Restored digitsshows the value that was active before the script. - Diagnostic check: run
say 1/3withNUMERIC DIGITS 5and again withNUMERIC DIGITS 20; the displayed decimal should differ in length.
Risks
- If the script stops before the restore lines, later calculations in the same session use the precision you set.
- Division rounds to
NUMERIC DIGITS; it does not truncate. Use the integer division operator%and remainder operator//when exact quotient semantics are required. - Very large digit settings slow arithmetic and consume more memory.
Notation form comparison
| Setting | Typical use |
|---|---|
NUMERIC FORM SCIENTIFIC | Exponential notation for general scientific results |
NUMERIC FORM ENGINEERING | Exponents aligned to multiples of three for units such as kilo and mega |
Error-safe restore
If a calculation can fail, wrap the block so the restore runs even when an error occurs. Arithmetic failures such as division by zero are often raised as SYNTAX conditions rather than ERROR, depending on the interpreter. Because condition names and behavior vary, verify the correct signal prefix in your target Rexx language reference before using this pattern in production.
saved_digits = DIGITS()
saved_form = FORM()
SIGNAL ON SYNTAX NAME error_handler
NUMERIC DIGITS 30
/* calculation that might fail */
NUMERIC DIGITS saved_digits
NUMERIC FORM saved_form
EXIT 0
error_handler:
say 'Error on line' SIGL 'code' RC
NUMERIC DIGITS saved_digits
NUMERIC FORM saved_form
EXIT RC
Limitations
NUMERIC DIGITSchanges persist for the routine or session; always save and restore.- The Rexx language specification is associated with ANSI X3.274-1996, but default digit counts, form behavior, and condition names can differ by implementation. Confirm version-specific defaults with your interpreter's documentation.
- This guide covers established Rexx behavior; it does not cover implementation-specific extensions.
Practical verification
After running the script, confirm that DIGITS() returns the pre-script value and that a subsequent unrelated calculation produces results at that original precision. If it does not, the restore step did not execute or the interpreter state was changed elsewhere in the session.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.