Newsletters   Subscriptions  Forums  Store   Career  Media Kit  About Us  Contact  Search   Home 
fhg
Volume 4, Number 34 -- October 13, 2004

Handling Oversized Numbers Gracefully

Hey, Ted:


These MCH1210 errors are driving me nuts. We run packaged software and a lot of the database fields are bigger than we need them to be. I often use smaller fields on displays and reports in order to save room. Then one day, for some reason, a field has a bigger value than I expected and an EVAL command blows up with MCH1210 (the target for a numeric operation is too small to hold the result). Is there any way in all this new-fangled RPG stuff to tell the system to print hash marks and keep on trucking?

--Dave


RPG has no such logic, but you can handle this situation yourself with little trouble. Since you want to display or print hash marks, you must define output fields as character data rather than as numeric data. You'll have to use the %EDITC and %EDITW functions within your programs to edit the fields (that is, apply decimal points, currency symbols, minus signs, commas, and the like). And you need a routine that can decide whether an edited number can fit into the space you've allowed for it.

The following example contains such a routine, which I call FmtNum (Format Numeric). It requires two parameters: an edited numeric value and the number of bytes into which the edited numeric value is to fit.

H dftactgrp(*no) actgrp(*new)

D FmtNum          pr           256    varying
D  EditedNum                   256    varying value
D  Size                         10u 0         value

D Column01        s              6a
D Column02        s              4a
D OrderQty        s              7p 0
D Balance         s             11p 2

 /free
     evalr Column01 = FmtNum(%editc(Balance: 'J'):
                             %size(Column01));
     evalr Column02 = FmtNum(%editc(OrderQty: '4'):
                             %size(Column02));
     *inlr = *on;
 /end-free
 * ===========================================================
P FmtNum          b
D FmtNum          pi           256    varying
D  EditedNum                   256    varying value
D  Size                         10u 0         value
D
 * local variables
D TrimmedNum      s            256    varying
D FillChar        s            256    inz(*all'#')

 /free
    if Size = *zero;
        return *blanks;
    endif;

    if Size > %size(FillChar);
       return *blanks;
    endif;

    TrimmedNum = %triml(EditedNum);
    if %len(TrimmedNum) <= Size;
       return TrimmedNum;
    endif;

    return %subst(FillChar:1:Size);
 /end-free

P FmtNum          e

In this example, OrderQty and Balance represent database fields and/or calculated variables. Column01 and Column02 represent fields on a display or report. Since there are two values to be copied into output fields, the main routine calls FmtNum twice, each time passing the edited numeric value and the size of the variable into which the edited value is to be placed. If the receiving variable is too small to hold the significant portion of the edited numeric value (that is, the portion of the edited numeric value without leading blanks), FmtNum returns enough hash marks to fill up the receiving variable. Notice that I used the EVALR op code in the main calculations in order to right-adjust the numeric values.

You could use the MONITOR, ON-ERROR, and ENDMON op codes to handle arithmetic overflow, but you would have to place such a structure around every EVAL. I much prefer the FmtNum subprocedure.

--Ted

Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com


Technical Editors: Howard Arner, Joe Hertvik, Ted Holt,
Shannon O'Donnell, Kevin Vandever
Managing Editor: Shannon Pastore
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Marc Logemann, David Morris
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.


THIS ISSUE
SPONSORED BY:

T.L. Ashford
ProData Computer Svcs
WorksRight Software


BACK ISSUES

TABLE OF
CONTENTS
Anatomy of a P-Field

Use System Naming Convention and Library List with .NET Managed Provider

Handling Oversized Numbers Gracefully


The Four Hundred
Q&A: iSeries GM Borman to Focus on i5/OS Sales

Dataram Sells Clone eServer i5, p5 Main Memory

IBM Talks Up WebSphere 6, Due in Two Months

Four Hundred Stuff
Hit Your New OS/400 Apps Hard with a TestBENCH 'Stress Test'

WorksRight Drives Down Mailing Costs with Three New Products

HiT Software Updates SQL Middleware for i5/OS Database

Four Hundred Monitor


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc. (formerly Midrange Server), 50 Park Terrace East, Suite 8F, New York, NY 10034
Privacy Statement