📄 ch14.htm
字号:
field. The <TT>V</TT> itself is output.
</UL>
<H3><A NAME="UsingConversionExits">
Using Conversion Exits</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <TT>conversion exit</TT> is a called routine that formats the
output. An edit mask that begins with <TT>==</TT> followed by
a four-character ID calls a function module that formats the output.
That four-character ID is known as a <I>conversion exit</I>, or
<I>conversion routine</I>. (Function modules are covered in a
subsequent chapter, but, for now, think of a function module as
a subroutine within another program.) The name of the function
module will be <TT>CONVERSION_EXIT_<I>XXXX</I>_OUTPUT</TT>,
where <TT><I>XXXX</I></TT> is
the four-character id that follows <TT>==</TT>. For example, <TT>write
'00001000' using edit mask '==ALPHA' </TT>calls the function module
<TT>CONVERSION_EXIT_ALPHA_OUTPUT</TT>. The <TT>write</TT> statement
passes the value first to the function module, which changes it
in any desired way and then returns the changed value, and that
value is written out. This particular exit (<TT>ALPHA</TT>) examines
the value to determine whether it consists entirely of numbers.
If it does, leading zeros are stripped and the number is left-justified.
Values containing non-numerics are not changed by <TT>ALPHA</TT>.
SAP supplies about 60 conversion exits for various formatting
tasks. After <A HREF="../ch19/ch19.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch19/ch19.htm" >Chapter 19</A>, "Modularization: Function Modules,
Part 1," on function modules, you will know enough to be
able to create your own conversion exits.
<P>
Conversion exits are particularly useful for complex formatting
tasks that are needed in more than one program. To illustrate,
the conversion exit <TT>CUNIT</TT> automatically converts the
code representing a unit of measure into a description meaningful
in the current logon language. For example, within R/3, the code
for a crate of materials is <TT>KI</TT>. (<TT>KI</TT> is an abbreviation
of <I>kiste</I>, the German word for "box.") However,
<TT>CUNIT</TT> converts <TT>KI</TT> to the English mnemonic <TT>CR</TT>.
Therefore, when logged on in English, <TT>write: '1', 'KI' using
edit mask '==CUNIT'.</TT> writes <TT>1 CR</TT>. For a user signed
on in German, the output would be <TT>1 KI</TT>. Using such a
conversion exit enables a standard set of codes stored in the
database to be automatically converted to be meaningful to the
current logon language whenever output to a report.
<H4>Conversion Routines within a Domain</H4>
<P>
A conversion exit can also be placed into the Convers. Routine
field in a domain. The exit is inherited by all fields that use
that domain, and will be automatically applied when the value
is written out. Figure 14.2 shows that domain <TT>ztxlifnr</TT>
uses the conversion routine <TT>ALPHA</TT>.
<P>
<A HREF="javascript:popUp('f14-2.gif')"><B>Figure 14.2 :</B> <I>Notice the contents of the Convers. Routine
field</I>.</A>
<P>
Therefore, whenever <TT>ztxlfa1-lifnr</TT> is written, the value
first passes through the function module <TT>CONVERSION_EXIT_ALPHA_OUTPUT</TT>,
which strips leading zeros from numeric values.
<P>
A conversion exit within a domain can be turned off by specifying
<TT>using no edit mask</TT> on the <TT>write</TT> statement.
<P>
Listing 14.8 illustrates the use of conversion exits.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 14.8 Using Conversion Exits<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1408.
2 tables: ztxlfa1, ztxmara.
3 data: f1(10) value '0000001000'.
4
5 write: / f1,
6 / f1 using edit mask '==ALPHA'.
7
8 skip.
9 select * from ztxlfa1 where lifnr = '00000010000'
10 or lifnr = 'V1'
11 order by lifnr.
12 write: / ztxlfa1-lifnr, "domain contains convers. exit ALPHA
13 / ztxlfa1-lifnr using no edit mask.
14 endselect.
15
16 skip.
17 select single * from ztxmara where matnr = 'M103'.
18 write: / ztxmara-matnr,
19 (10) ztxmara-brgew,
20 ztxmara-gewei. "domain contains convers. exit CUNIT
21 set locale language 'D'.
22 write: / ztxmara-matnr,
23 (10) ztxmara-brgew,
24 ztxmara-gewei. "domain contains convers. exit CUNIT
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 14.8 produces this output:
<BLOCKQUOTE>
<PRE>
0000001000
1000
1000
0000001000
V1
V1
M103 50.000 CR
M103 50.000 KI
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 5 writes the value from <TT>f1</TT>.
<LI>Line 6 writes the same value but applies conversion exit <TT>ALPHA</TT>.
Before being written, <TT>f1</TT> is passed to the function module
<TT>CONVERSION_EXIT_ALPHA_OUTPUT</TT>, which detects that the
value is entirely numeric and strips off leading zeros and left-justifies
it.
<LI>Line 9 retrieves two records from <TT>ztxlfa1and</TT> and
writes the <TT>lifnr</TT> field twice for each.
<LI>The domain for <TT>ztxlfa1-lifnr</TT> contains the conversion
exit <TT>ALPHA</TT>, so line 12 writes <TT>lifnr</TT> using that
conversion exit.
<LI>Line 13 writes <TT>lifnr</TT> out, but turns off the conversion
exit. This causes the actual value in the table to be written
out.
<LI>Line 17 selects a single record from <TT>ztxmara</TT>.
<LI>Line 20 writes <TT>gewei</TT> (unit of weight) using conversion
exit <TT>CUNIT</TT> in the domain.
<LI>Line 21 changes the text environment to German. This is equivalent
to logging on in German.
<LI>Line 24 writes the same <TT>gewei</TT> value, but this time
<TT>CUNIT</TT> displays the German mnemonic.
</UL>
<H4>Displaying Existing Conversion Exits</H4>
<P>
To display the existing conversion exits, use the following procedure.
<P>
<img src="../button/screencam.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/screencam.gif">
<P>
Start the ScreenCam "How to Display Existing Conversion Exits"
now.
<OL>
<LI>From the Development Workbench screen, choose the menu path
Overview->Repository Infosys. The ABAP/4 Repository Information
System screen is displayed.
<LI>Click on the plus (+) beside Programming. The tree expands.
<LI>Click on the plus (+) beside Function Library. The tree expands.
<LI>Double-click on the line <TT>Function Modules</TT>. The ABAP/4
Repository Information System: Function Module screen is displayed.
<LI>In the Function Module field, enter <B>CONVERSION_EXIT_*_OUTPUT</B>
(the field is scrollable; you can enter more characters into it
than it can display).
<LI>Press the Execute button on the Application toolbar. The Function
Modules screen is displayed. All of the conversion exits that
can be used on the <TT>write</TT> statement will be listed here.
</OL>
<H3><A NAME="WorkingwithDateFormatting">
Working with Date Formatting</A></H3>
<P>
Date formatting specifications, for the most part, do <I>not</I>
do what you think they will. Most pull their format from the Date
Format specification in the user defaults (menu path System->User
Profile->User Defaults).
<P>
Table 14.5 contains a description of the effect of each.<BR>
<P>
<CENTER><B>Table 14.5 Effects of the Date Format Specifications</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Format</B></CENTER></TD><TD WIDTH=384><CENTER><B>Effect</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>mm/dd/yyyy dd/mm/yyyy</TT></TD><TD WIDTH=384>Writes out the date using the format specified in the user's profile. This is the default, so these formats do not actually do anything.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>mm/dd/yy
<BR>
dd/mm/yy</TD><TD WIDTH=384>Writes out the date with a two-character year using the format specified in the user's profile. They both do the same thing.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>Mmddyy<BR>
ddmmyy</TD><TD WIDTH=384>Writes out the date using a two-character year using the format specified in the user's profile, without separators. They both do the same thing.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>Yymmdd</TT></TD><TD WIDTH=384>Writes out the date in yymmdd format without separators.
</TD></TR>
</TABLE>
</CENTER>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Conversion exits <TT>IDATE </TT>and <TT>SDATE </TT>provide alternate date formats. Also, the function module <TT>RH_GET_DATE_DAYNAME </TT>returns the name of the day of the week for a given date and language. As an alternative, <TT>DATE_COMPUTE_DAY </TT>returns a number representing the day of the week.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 14.9 illustrates the use of the date format specifications.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 14.9 Using Date Format Specifications<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1409.
2 data: f1 like sy-datum value '19981123',
3 begin of f2,
4 y(4),
5 m(2),
6 d(2),
7 end of f2,
8 begin of f3,
9 d(2),
10 m(2),
11 y(4),
12 end of f3,
13 begin of f4,
14 m(2),
15 d(2),
16 y(4),
17 end of f4.
18
19 write: / f1, "user profile sets format
20 / f1 mm/dd/yyyy, "user profile sets format
21 / f1 dd/mm/yyyy, "user profile sets format
22 / f1 mm/dd/yy, "user profile fmt, display with 2-digit year
23 / f1 dd/mm/yy, "user profile fmt, display with 2-digit year
24 / f1 mmddyy, "user profile fmt, 2-digit year, no separators
25 / f1 ddmmyy, "user profile fmt, 2-digit year, no separators
26 / f1 yymmdd, "yymmdd format, no separators
27 / f1 using edit mask '==IDATE',
28 /(11) f1 using edit mask '==SDATE'.
29
30 f2 = f1.
31 move-corresponding f2 to: f3, f4.
32 write: /(10) f3 using edit mask '__/__/____', "dd/mm/yyyy format
33 /(10) f4 using edit mask '__/__/____'. "mm/dd/yyyy format
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 14.9 produces this output:
<BLOCKQUOTE>
<PRE>
1998/11/23
1998/11/23
1998/11/23
98/11/23
98/11/23
981123
981123
981123
1998NOV23
1998/NOV/23
23/11/1998
11/23/1998
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 defines <TT>f1</TT> like <TT>sy-datum</TT>. The output
length in the domain for <TT>sy-datum</TT> is 10, so separators
will be displayed when the value of <TT>f1</TT> is written out.
<LI>Lines 19, 20, and 21 all do the same thing: They write <TT>f1</TT>
using the format specified in the user's profile.
<LI>Lines 22 and 23 both do the same thing: They write <TT>f1</TT>
using the format specified in the user's profile, but using a
two-digit year.
<LI>Lines 24 and 25 both do the same thing: They write <TT>f1</TT>
using the format specified in the user's profile using a two-digit
year, but without separators.
<LI>Line 27 formats the date using the conversion exit <TT>IDATE</TT>.
The output of <TT>IDATE</TT> is yyyymmmdd format.
<LI>Line 28 formats the date using the conversion exit <TT>SDATE</TT>.
The output of <TT>SDATE</TT> is yyyy/mmm/dd format.
<LI>Lines 30 through 33 show how to create your own output format.
If you were to do this in real life, it would be better to put
this code into a conversion exit and then name the conversion
exit on the <TT>write</TT> statement. Not only would you be able
to simplify your code, but it would also be available to you and
all other programmers for instant application in any program.
</UL>
<H3><A NAME="UsingthenozeroandnosignAdditions">
Using the no-zero and no-sign Additions</A></H3>
<P>
The <TT>no-zero</TT> addition suppresses leading zeros when used
with type <TT>c</TT> or type <TT>n</TT> variables. In the case
of a zero value, the output is all blanks.
<P>
The <TT>no-sign</TT> addition, when used with variables of type
<TT>i</TT>, <TT>p</TT>, or <TT>f</TT>, suppresses the output of
the sign character. In other words, negative numbers will not
have a sign and will appear as if they were positive.
<P>
Listing 14.10 illustrates the use of these two additions.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 14.10 Using no-zero and no-sign<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1410.
2 data: c1(10) type c value '000123',
3 n1(10) type n value 123,
4 n2(10) type n value 0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -