× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



For the math-challenged (assuming externally described printer file):

d pfldRange ds qualified
d min like( pfld ) inz( *loval )
d max like( pfld ) inz( *hival )

if val < pfldRange.min ;
pfld = pfldRange.min ;
elseif pval > pfldRange.max ;
pfld = pfldRange.max ;
else
pfld = pval ;
endif;

No fancy math required. If pfld is set in many places, enclose in a
procedure. Also has the advantage of automatic adaptability
(compile-time) to changes to the width of pfld; no coding of magic
numbers that need to be kept in sync with the printer file field size.


On 9/14/07, Mike Krebs <mkrebs@xxxxxxxxxxxxxxxxxx> wrote:
Here is the prodecure that worked for the current report (I didn't
exhaustively test it). This procedure will format a large number to fit into
a smaller variable (shrink variable size for printing).

The key calc is setting the max value (thanks Jonathan for remembering your
algebra!). It uses exponential notation to calculate a number that is just
bigger than the target and then subtracts the amount that will return all
nines for the right size of the print field. A quick review of some algebra
web pages reveals that any number (except 0) with exponent 0 is defined as
1. This allows the calc to work for zero decimal positions. A negative
exponent for the number ten will have that many decimals in the result
(-1=.1, -2=.01, -3=.001, etc).

For example:
Size of Fld Num of Dec maxValue Calc
%len(field) %decpos(field) (10 ** (length-decPos))-(10 ** (-1 * (decpos)))
1 0 (10**1)-(10**0) =10-1 =9
7 0 (10**7)-(10**0) =10,000,000-1=9,999,999
7 1 (10**6)-(10**-1)=1,000,000-.1=999,999.9
7 4 (10**3)-(10**-4)=1000-.0001 =999.9999
3 3 (10**0)-(10**-3)=1-.001 =.999

Mike Krebs

//--------------------------------------------------
// Procedure name: GetPrintVal
// Purpose: Set print value according to size of field
// Returns: Print value
// Parameter: inValue => Value to check
// Parameter: length => Length of print field
// Parameter: decPos => Decimal positions
//--------------------------------------------------
D GetPrintVal PR 60P10
D inValue 60P10 CONST
D length 5I 0 CONST
D decPos 5I 0 CONST


//--------------------------------------------------
// Procedure name: GetPrintVal
// Purpose: Set print value according to size of field
// Returns: Print value
// Parameter: inValue => Value to check
// Parameter: length => Length of print field
// Parameter: decPos => Decimal positions
//--------------------------------------------------
P GetPrintVal B
D GetPrintVal PI 60P10
D inValue 60P10 CONST
D length 5I 0 CONST
D decPos 5I 0 CONST

// Local fields
D maxValue S 60P10
D minValue S 60P10

/FREE
maxValue = (10 ** (length-decPos))-(10 ** (-1 * (decpos)));
minValue = 0 - maxValue;
if inValue > maxValue;
return maxValue;
EndIf;
if inValue < minValue;
return minValue;
EndIf;
return inValue;

/END-FREE
P GetPrintVal E

Here is an example of how you use it.
D printField s 7 0
D extOHCost s 21 6
D onHand s 9 0
D baseCost s 13 6

/FREE
printField =
GetPrintVal(onHand*baseCost:%len(printField):%decpos(printField));
(or like this)
extOHCost = onHand * baseCost;
printField=
GetPrintVal(extOHCost:%len(printField):%decpos(printField));
/END-FREE

--
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.





As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.