Return to the RPG Tips
Convert String to Number using C Functions
The code below shows how to use C functions atoi (ASCII to Integer) and atof (ASCII to Floating point) to convert an alphameric string to a number. Note that atoi expects only digits (no decimals, signs, or commas), while atof can convert numbers containing a decimal, +/- sign, and commas. h bnddir('QC2LE') D atoi pr 10i 0 extproc('atoi') D num * options(*string) value D atof pr 8f extproc('atof') D num * options(*string) value D i s 10i 0 D p s 13p 7 C movel '-100 ' num 6 C eval i = atoi(%trim(num)) * > EVAL i * I = -100 C eval(h) p = atof(%trim(num)) * > EVAL i * P = -000100.0000000 C movel '-5.67 ' num 6 C eval(h) p = atof(%trim(num)) * > EVAL p * P = -000005.6700000 C return Code provided by IBM's Barbara Morris
[report a broken link by clicking here]