fhg
Volume 9, Number 13 -- April 15, 2009

A Bevy of BIFs: %CHAR, %EDITC and %EDITW

Published: April 15, 2009

by Jon Paris

"I need to convert a numeric value into a character string. How do I . . . ?"

This type of question is often asked by RPGers in these days of comma separated value files, XML documents, and Web pages (HTML). Generating such files has become a common requirement in the lives of RPGers. If they aren't yet commonplace in your corner of the world, then they soon will be; if that doesn't happen soon, maybe you should be concerned about the future of your favorite platform in your organization!

So let's start by taking a quick look at one of my favorite built-in functions: %Char. Originally introduced to handle conversion of date fields to a character form, %Char was quickly upgraded for use with numeric fields and expressions. But the way in which it operates often catches programmers by surprise. Why? Because %Char performs leading zero suppression on the input and then trims the resulting string to remove leading and trailing blanks. As a result, the length of the resulting string varies depending on the value in the field being converted. As we will see later, this is very different from %EDITC (and its companion %EDITW), which will always produce identical length output from a given input field definition and edit code combination--regardless of the actual content of the field.

As a result, when you run the code below, the first DSPLY message (A) will say "%Char value is (12.34)" and the second (B) "%Char value is (10012.34)". When you are building text such as that used in the message here, this is a good thing. It saves you having to worry about stripping the blanks etc. It will even place a minus sign in front of negative numbers and, as you can see, will insert a decimal point when the expression contains decimal places. Very useful.

But what if you want to simply convert a numeric value to its character equivalent? For example, to replace an old-fashioned MOVE operation. If you try to use %Char for that purpose you will be disappointed for many reasons. For one you may find an unwanted decimal point inserted as in our examples, and for another, because you'll either end up with trailing spaces in the output field (C) " Result in char field is (12.34 )", or worse still, truncation of the decimal places (D) " Result in char field is (10012.3)" and that's probably not what you expect, or indeed want.

Replacing the MOVE Operation

So how do you handle the requirement to replace a MOVE operation? The answer is %EditC. %EditC applies an edit code to a numeric field just as it would if you used that code on an O-spec or in a DDS definition. It then gives you back the edited character string for inclusion in whatever other text you want. So what's the magic code that allows us to insert leading zeros and not have to worry about decimal place insertion etc? 'X' marks the spot! The X edit code will retain leading zeros, and will not insert a decimal point, comma, or anything else. The result is that our test program will output (E) "%EditC value is (0001234)" and (F) "%EditC value is (1001234)". Note that the length of the display string is identical in each case and any negative values will be indicated by an alpha character in the right-most position of the field just as they would have been with a MOVE.

We won't go into details of all of the edit code options available. As we noted earlier they are identical to those used on O-specs and in DDS, as are the edit words used by %EditW. Having said that, there is one difference between the way certain editing requirements are specified with %EditC relative to O-specs. If you require the resulting string to have leading zeros replaced by asterisks, then the keyword *ASTFILL should be specified as the third parameter to %EditC. Similarly, if you want a floating currency sign to be added to the field, you can specify *CURSYM for the default value, or enter a single character of your own choosing to have that act as the floating currency symbol.

As you can see, there's a lot you can do with these BIFs - so if you have been using O-specs to format this kind of data, perhaps it is time to revise your coding techniques.

     D  testNum         S               7p 2 inz(12.34)
     D  testChar        S               7a

      /Free
(A)    Dsply  ('%Char value is (' + %Char(testNum) + ')');

       testNum += 10000;

(B)    Dsply  ('%Char value is (' + %Char(testNum) + ')');

       testNum = 12.34; // Reset to original value

       // Attempt to use %Char() as equivalent to MOVE
       testChar = %Char(testNum);

(C)    Dsply  ('Result in char field is (' + testChar + ')');

       testNum += 10000;

       testChar = %Char(testNum);

(D)    Dsply  ('Result in char field is (' + testChar + ')');

       testNum = 12.34; // Reset to original value

(E)    Dsply  ('%EditC value is (' + %EditC(testNum: 'X') + ')');

       testNum += 10000;

(F)    Dsply ('%EditC value is (' + %EditC(testNum: 'X') + ')');

       *InLR = *On;


Jon Paris is one of the world's most knowledgeable experts on programming on the System i platform. Paris cut his teeth on the System/38 way back when, and in 1987 he joined IBM's Toronto software lab to work on the COBOL compilers for the System/38 and System/36. He also worked on the creation of the COBOL/400 compilers for the original AS/400s back in 1988, and was one of the key developers behind RPG IV and the CODE/400 development tool. In 1998, he left IBM to start his own education and training firm, a job he does to this day with his wife, Susan Gantner--also an expert in System i programming. Paris and Gantner, along with Paul Tuohy and Skip Marchesani, are co-founders of System i Developer, which hosts the new RPG & DB2 Summitconference. Send your questions or comments for Jon to Ted Holt via the IT Jungle Contact page.


RELATED STORIES

A Bevy of BIFs: %SCAN and %CHECK

A Bevy of BIFs: Look Up to %LookUp

A Bevy of BIFs: Getting a Date is Easy with %Date



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
VISION SOLUTIONS


FINALLY!


All i5/OS and AIX data protection and
recovery options described in one document!


Click here to download this comprehensive 29-page white paper



Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

MKS:  FREE white paper: What Do IBM i Developers Want Out of Their ALM Software?
S4i Systems:  Say YES to DASD-Plus. Disk management starting at $350
COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
Power vs. Nehalem: Time to Double Up and Double Down

The IBM-Sun Saga Continues--Or Rather, Doesn't

Clone Memory Maker Dataram Buys Rival MMB

Lawson Ekes Out Profit in Q3, Partners to Peddle in Quebec

Automation: Monitoring the Monitors Isn't It

Four Hundred Stuff
Fujitsu Introduces RPG to .NET Application Modernization Service

LANSA Adds Refinements to BPI Product

SkyView Adds PCI Checks to Risk Assessment Tool

SPSS Changes Data Miner's Name, Drops System i Support

So Long ASNA--It's BluePhoenix System i Division From Now On

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
April 11, 2009: Volume 11, Number 15

April 4, 2009: Volume 11, Number 14

March 28, 2009: Volume 11, Number 13

March 21, 2009: Volume 11, Number 12

March 14, 2009: Volume 11, Number 11

March 7, 2009: Volume 11, Number 10

TPM at The Register
Big-iron brains powers Schooner appliance power

Dell adds VC hotshot to board

IBM serves System S streaming super

Sun software - Does it make diddly?

Nehalem aces OLTP test on HP iron

Cisco shells out $105m for Tidal

Microsoft puts Sun's Tremblay in SiArch

Novell's openSUSE does ARM Linux

Sun Sparc guru splits for Redmond

Sun revs VirtualBox to 2.2

Double-Take expands beyond server replication

Blade server standards coming this year

Apple plays catchup with Nehalem EP-powered Xserves

Egenera grooms Nehalem EP blades

THIS ISSUE SPONSORED BY:

Help/Systems
Vision Solutions
WorksRight Software


Printer Friendly Version


TABLE OF CONTENTS
A Bevy of BIFs: Dealing with a Bad Date

A Bevy of BIFs: %CHAR, %EDITC and %EDITW

Admin Alert: And /QOpenSys and /QOpenSys and /QOpenSys and. . .

Reader Feedback: Programming E-mail Notification When a Robot/SCHEDULE Job Fails

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
PHP CLI Call

Perl, PHP, and/or ZendCore

batch printing PDF files from RPG program

Using db2_connect in PHP on iSeries

How to return value from CL program?

ADO.NET/IBM.Data.DB2.iSeries/ iDB2Connection

Order by alias names




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement