enskill.com iSeries Training Onsite classes
DVD training
Books
Online e-Learning
Home
Resources
Contact
(c)2002-2008

This page is powered by Blogger. Isn't yours? Bryan's RPG IV Journal


Using %DEC to Convert Character to Numeric


Q. In your December 2004 article, "10 Cool Things About RPG IV," I could not get the %XLATE('$*,':' ':Amount) line to work. Testing the %XLATE with constant fields and stepping through it, I found the $ would be replaced by a space, but the asterisk and comma were left in. The only way it would work was to do three %Xlate functions - removing only one of the characters at a time. Did I do something wrong?

A. There was a printing error in that tip. The second %XLATE argument must have as many substitution characters as the search (first) argument specifies. So, there should have been three blanks in the second argument, to match the '$*,' in the first argument.

Here's a corrected and expanded version of the tip:

At Version 5 Release 2, the %DEC function converts a string expression to a packed decimal number, with a specified precision:
%DEC(expression:digits:decimals)
Previous releases allowed you to use only numeric expressions with %DEC. When you use the %DEC function to convert a string, the second and third parameters are required.

If the character field Amount has a value of '00123.45' you could convert it to a packed decimal number by coding
%DEC(Amount:7:2)
If the string value includes non-numeric characters (other than a decimal character), you can include the %XLATE function to translate those characters to blanks. For example, if Amount has a value of '$***12,345.67 ' you could code
%DEC( %XLATE('$*,':'   ':Amount) : 7 : 2 )
to first convert the currency symbol, asterisks, and commas to blanks. The %DEC function doesn't mind the embedded blanks, so your program would get a seven digit packed return value of 12345.67 .

If the %DEC function encounters an invalid numeric value, it will return a status code of 105, which you can easily capture:
MONITOR;
  Result = %DEC(%XLATE('$*,':' ':Amount):7:2);
ON-ERROR 105;
  Result = 0;
ENDMON;
To round the numeric result, use %DECH. The %INT and %INTH functions also accept string arguments, returning integer values.


Log of Journal Entries

Use this scrollable blog map to view other journal entries:
Journal Home
It is your responsibility to ensure procedures, techniques, and code used from this website are accurate and appropriate for your installation. No warranty or support is implied or expressed.
E-mail to Bryan Meyers
Privacy and Email Policy
 
AS/400®, eServer, i5/OS, IBM i, Integrated Language Environment®, iSeries, OS/400®, RPG/400®, System i5, VisualAge®, and WebSphere® are trademarks of IBM Corporation. Note: IBM® System i is the latest member of the family of eServer iSeries. This site might refer to System i as System i5, iSeries, or AS/400.
 

If you are having trouble navigating the menu at the top of this page, you may use the following scrollable site map instead:
 

Self-paced DVD Training
DVD: RPG IV for RPG Programmers
DVD: RPG IV Modules, Procedures, and Service Programs
 
In association with Amazon.com
Programming in RPG IV
RPG IV Jump Start
Power Tips for RPG IV
Control Language Programming for the AS/400
VisualAge for RPG by Example
 
International visitors: click hereInternational visitors:
Click here!