ILERPG.com — IBM i · ILE RPG · AS400 Mastery
2 min read· Admin

Numbers and Dates in ILE RPG: Convert, Measure, Project

An amount arrives as text from an external interface. A date sits in a file as eight plain digits. Two timestamps exist with no built-in notion of how far apart they are. The built-in functions covered in this lesson close each of those gaps.

Converting numbers

%DEC(value : digits : decimals) converts text or numbers into packed decimal at an explicit precision, truncating extra decimal positions. %DECH performs the same conversion but half-adjusts, which is what a computed tax amount headed for a two-decimal field usually needs. %INT and %UNS convert to whole numbers by truncation; %UNS raises a runtime error on a negative value, so validate before converting.

taxAmount = %dec('899.995' : 9 : 2);    // 899.99
taxAmount = %dech('899.995' : 9 : 2);   // 900.00

Converting back to text

%CHAR turns a numeric, date, time, or timestamp value into a string — typically the last step before a value reaches a screen, a message, or a log. An optional format such as *USA or *ISO controls how a date is rendered.

Building real dates from raw input

%DATE, %TIME, and %TIMESTAMP construct validated values from digits or text, given a format that describes how to read the input. The format must match the actual digit order of the source. A legacy field in month-day-year order read as *ISO produces a valid-looking but wrong date — the kind of defect that surfaces later, in a report that does not reconcile.

parsedDate = %date(legacyDateNum : *iso);   // 20260913 -> 2026-09-13

Measuring versus projecting

%DIFF(value1 : value2 : unit) answers "how far apart are these two points?" in days, months, hours, or finer units, and it is calendar-aware. %DAYS, %MONTHS, and %YEARS answer the opposite question: "where do I land if I move this far from here?" A ship-by date or a policy renewal date is a projection; an age-of-invoice figure or an SLA in hours is a measurement.

Lesson 19 closes with a policy renewal routine that uses nearly every one of these functions, each for one distinct job.

Get the lesson

Lesson 19, “Built-In Functions Part 2: Numeric and Date/Time,” is part of Beginner ILE RPG in IBM i ILE RPG Mastery and is available at ILERPG.com for USD 10. The complete Beginner ILE RPG series — all 18 lessons — is USD 180, or USD 135 with coupon code RPG30, which takes 25% off when you purchase the full series.