fhg
Volume 6, Number 2 -- January 11, 2006

Indicate Negative Numbers with Parentheses

Published: January 11, 2006

Hey, Ted:

Thank you for a newsletter with information that is always interesting and useful. I enjoyed reading about your IIF function. Here is a function I wrote a few years back on V5R1. It returns a numeric value edited for displaying or printing that includes parenthesis when the value is negative. Our accountants prefer that negative numbers appear within parenthesis. The value returned contains two decimals, as this function is primarily used for monetary amounts.

--Glenn T. Berg, programmer/analyst


It was good of Glenn to share his EditParens function with the rest of us. I don't blame the bean counters for wanting to see negative numbers in parentheses. Those trailing minus signs don't stand out as well, and can get lost when numbers are displayed or printed close together. If a number is negative, EditParens places a floating opening parenthesis immediately before the first non-blank character of the edited result and follows the edited number with a closing parenthesis. If the number is not negative, the positions that the parentheses would fill are left blank.

Here is the procedure prototype for the EditParens function. In my testing, I put it in member EditParens of source physical file PROTOTYPES.

D* Prototype for Subprocedure EditParens     
D EditParens      PR            42A   VARYING
D    Val                        30P 2 VALUE

Here's the function itself.

*-----------------------------------------------------------------------
*  Subprocedure EditParens - Place Parentheses Around Negative Number   
*-----------------------------------------------------------------------
H nomain                                                                 
                                                                         
D/copy prototypes,EditParens                                             
                                                                         
P EditParens      B                   EXPORT                             
                                                                         
D* Procedure Interface                                                   
D EditParens      PI            42A   VARYING                            
D  Val                          30P 2 VALUE                              
D* Local Work fields                                                     
D Lparen          C                   CONST('(')                         
D Rparen          C                   CONST(')')                         
D WRK             S             42A   VARYING                            
                                                                         
 *    Edited Value
 *   Note that the '1' edit code does not include a sign so we 
 *   don't have to calculate the absolute value when negative. 
C                   If         Val >=0                         
C                   Eval       WRK = %TrimL(%Editc(Val:'1')) + 
C                              ' '                             
C                   Else                                       
C                   Eval       WRK = Lparen +                  
C                              %TrimL(%Editc(Val:'1')) +       
C                              RParen                          
C                   End                                        
                                                               
C                   Return     WRK                             
P                 E

Here's an example of how a calling routine might invoke EditParens. Numeric variables WXCUR and WXYTD are copied into edited variables WXCURO and WXYTDO.

D WXYTD           s              9P 2                   
D WXCUR           s              7P 2                   
D WXYTDO          s             14A                     
D WXCURO          s             11A                     

D/copy prototypes,EditParens                            
                                                        
C                   EvalR     WXYTDO = EditParens(WXYTD)
C                   EvalR     WXCURO = EditParens(WXCUR)

I will leave it to the reader to place the routine in an appropriate module or service program. This is a topic I dealt with in the January 4 edition of Four Hundred Guru.

--Ted


RELATED STORIES

A Handy RPG FUNction

Properly Placed Procedures



Sponsored By
ADVANCED SYSTEMS CONCEPTS

SEQUEL can be used for virtually ALL data access functions on the iSeries.

A Windows-based user interface makes it easy to design queries and reports.

SEQUEL offers executive dashboards, drill-down data analysis and run-time prompts to deliver important iSeries data to managers and other non-technical users.

E-mail and FTP delivery let you deliver information to remote users and servers.

www.asc-iseries.com



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, 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.

Sponsored Links

nuBridges:  Leading provider of secure FTP on the iSeries
COMMON:  Join us at the Spring 2006 conference, March 26-30, in Minneapolis, Minnesota
California Software:  Migrate iSeries apps to Windows, Linux, or Unix

 


 
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-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement