📄 ch14.htm
字号:
5 i1 type i value '123-',
6 i2 type i value 123.
7 write: / c1, 20 'type c',
8 / c1 no-zero, 20 'type c using no-zero',
9 / n1, 20 'type n',
10 / n1 no-zero, 20 'type n using no-zero',
11 / n2, 20 'type n: zero value',
12 / n2 no-zero, 20 'type n: zero value using no-zero',
13 / i1, 20 'type i',
14 / i2 no-sign, 20 'type i using no-sign'.
</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.10 produces this output:
<BLOCKQUOTE>
<PRE>
000123 type c
123 type c using no-zero
0000000123 type n
123 type n using no-zero
0000000000 type n: zero value
type n: zero value using no-zero
123- type i
123 type i using no-sign
</PRE>
</BLOCKQUOTE>
<H3><A NAME="SpecifyingDecimalsandRounding">
Specifying Decimals and Rounding</A></H3>
<P>
Two statements perform rounding:
<UL>
<LI><TT>decimals</TT>
<LI><TT>round</TT>
</UL>
<P>
Three statements are used to specify the number of decimal places
in the output:
<UL>
<LI><TT>decimals</TT>
<LI><TT>currency</TT>
<LI><TT>unit</TT>
</UL>
<H4>Using the decimals and round Additions</H4>
<P>
The <TT>decimals <I>n</I></TT>
addition can be used to increase or decrease the number of decimal
places displayed in the output list. If you decrease the number
of decimal places, the value is rounded before being written to
the list. The source variable value is not changed, only the output
appears differently. If you increase the number of decimals, zeros
are appended to the end of the value. Negative values of <TT><I>n</I></TT>
move the decimal to the right, effectively multiplying the number
by factors of 10 and then rounding before display.
<P>
The <TT>round <I>n</I></TT> addition
causes the output to be scaled by factors of 10 and then rounded
before display. This has the same effect as multiplying by 10**<TT><I>n</I></TT>.
Positive <TT><I>n</I></TT> values
move the decimal to the left, and negative values move it to the
right. This is useful, for example, when you want to output values
to the nearest thousand.
<P>
The difference between <TT>decimals</TT> and <TT>round</TT> is
that <TT>decimals</TT> changes the number of digits seen after
the decimal point to <TT><I>n</I></TT>
and then performs rounding. <TT>round</TT> moves the decimal point
left or right by <TT><I>n</I></TT>,
maintaining the number of digits after the decimal, and then performs
rounding to the number of decimal digits displayed.
<P>
Listing 14.11 illustrates the use of <TT>decimals</TT> and <TT>round</TT>.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 14.11 Using the decimals and round Additions
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1411.
2 data f1 type p decimals 3 value '1575.456'.
3 write: / f1, 20 'as-is',
4 / f1 decimals 4, 20 'decimals 4',
5 / f1 decimals 3, 20 'decimals 3',
6 / f1 decimals 2, 20 'decimals 2',
7 / f1 decimals 1, 20 'decimals 1',
8 / f1 decimals 0, 20 'decimals 0',
9 / f1 decimals -1, 20 'decimals -1',
10 / f1 round 4, 20 'round 4',
11 / f1 round 3, 20 'round 3',
12 / f1 round 2, 20 'round 2',
13 / f1 round 1, 20 'round 1',
14 / f1 round 0, 20 'round 0',
15 / f1 round -1, 20 'round -1',
16 / f1 round -2, 20 'round -2',
17 / f1 round 3 decimals 1, 20 'round 3 decimals 1',
18 / f1 round 3 decimals 3, 20 'round 3 decimals 3'.
</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.11 produces this output:
<BLOCKQUOTE>
<PRE>
1,575.456 as-is
1,575.4560 decimals 4
1,575.456 decimals 3
1,575.46 decimals 2
1,575.5 decimals 1
1,575 decimals 0
158 decimals -1
0.158 round 4
1.575 round 3
15.755 round 2
157.546 round 1
1,575.456 round 0
15,754.560 round -1
157,545.600 round -2
1.6 round 3 decimals 1
1.575 round 3 decimals 3
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
On line 8, the <TT>decimals 0</TT> addition does not round up
because the fractional part of the number is less than .5.
<H4>Using the currency and unit Additions</H4>
<P>
The additions <TT>currency</TT> and <TT>unit</TT> are an alternate
way of indicating the position of the decimal point for a type
<TT>p</TT> field. They cannot be used with the <TT>decimals</TT>
addition.<p>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The <TT>DDIC</TT> types <TT>CURR </TT>and <TT>QUAN </TT>are based on the ABAP/4 data type p (packed decimal).
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<H5>Using the currency Addition</H5>
<BLOCKQUOTE>
When you write out a currency amount, you should also use the
<TT>currency</TT> addition. Such amounts are usually retrieved
from a type <TT>CURR</TT> field in a database table.
</BLOCKQUOTE>
<BLOCKQUOTE>
Use <TT>currency</TT> instead of <TT>decimals</TT> to specify
the number of decimal places for currency amount fields. Unlike
<TT>decimals</TT>, on which you specify the number of decimal
places, you instead specify a currency code, such as <TT>USD</TT>
(U.S. Dollars), <TT>ITL</TT> (Italian Lira), or <TT>JPY</TT> (Japanese
Yen). The system then looks up that currency code in table <TT>TCURX</TT>
to determine the number of decimal places that should be used
to display the value. If the currency code is not found in <TT>TCURX</TT>,
a default of two decimal places is used.
</BLOCKQUOTE>
<BLOCKQUOTE>
The currency code is usually also stored either in the same database
table or another, related one. For example, the currency key for
type <TT>CURR</TT> fields (<TT>saldv</TT>, <TT>solll</TT>, and
<TT>habnl</TT>) in table <TT>ztxlfc3</TT> is stored in the field
<TT>ztxt001-waers</TT>. You can find this out by double-clicking
on any of these fields. For example, if you double-click on <TT>ztxlfc3-saldv</TT>,
you will see the Display Field ZTXLFC3-SALDV screen shown in Figure
14.3.<BR>
<A HREF="javascript:popUp('f14-3.gif')"><B>Figure 14.3 :</B> <I>A double-click on field saldv in table
ztxlfc3 displays this screen</I>.</A>
</BLOCKQUOTE>
<BLOCKQUOTE>
The reference table and reference field indicate that <TT>ztxt001-waers</TT>
contains the currency key.
</BLOCKQUOTE>
<BLOCKQUOTE>
The table <TT>ztxt001</TT> itself contains the attributes of all
company codes; its primary key is <TT>bukrs</TT>. If you take
the value from <TT>ztxlfc3-bukrs</TT> and look up the single row
in <TT>ztxt001</TT> that matches, <TT>waers</TT> will contain
the currency key. Listing 14.12 illustrates.
</BLOCKQUOTE>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 14.12 Using the currency Addition<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1412.
2 tables: ztxlfc3.
3 data: f1 type p decimals 1 value '12345.6'.
4
5 write: / f1,
6 / f1 currency 'USD', 'USD', "US Dollars
7 / f1 currency 'ITL', 'ITL'. "Italian Lira
8
9 skip.
10 select * from ztxlfc3 where gjahr = '1997'
11 and saldv >= 100
12 order by bukrs.
13 on change of ztxlfc3-bukrs.
14 select single * from ztxt001 where bukrs = ztxlfc3-bukrs.
15 endon.
16 write: / ztxlfc3-saldv currency ztxt001-waers, ztxt001-waers.
17 endselect.
</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.12 produces this output:
<BLOCKQUOTE>
<PRE>
123,456
1,234.56 USD
123,456 ITL
100.00 USD
100.00 USD
100.00 USD
100.00 USD
100,000 ITL
25,050 ITL
100,000 ITL
100,000 JPY
300,000 JPY
250.50 CAD
200.00 CAD
1,000.00 CAD
100.00 CAD
</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 of <TT>f1</TT> as-is.
<LI>Line 6 writes the same value, but first it looks up <TT>'USD'</TT>
in table <TT>TCURX</TT> to determine the number of decimal places.
The value <TT>'USD'</TT> is not in that table, so the output is
formatted using the default (two decimal places).
<LI>When line 7 is executed, table <TT>TCURX</TT> indicates that
<TT>ITL</TT> has no decimal places and so formats the value without
any decimals.
<LI>Line 10 selects records from table <TT>ztxlfc3</TT> in company
code order (<TT>bukrs</TT>).
<LI>Line 13 is triggered each time the company code changes.
<LI>Line 14 looks up the company code in table <TT>ztxt001</TT>
to find the currency for that company. The currency key is available
in field <TT>ztxt001-waers</TT>.
<LI>Line 16 writes the balance carried forward using the number
of decimal places indicated by the currency key in field <TT>ztxt001-waers</TT>.
</UL>
<H5>Using the unit Addition</H5>
<BLOCKQUOTE>
The <TT>unit</TT> addition is used to specify the number of decimals
displayed for a quantity field. Units, such as <TT>cm</TT> (centimeters)
or <TT>lb</TT> (pounds), determine the number of decimal places
displayed. The code, such as <TT>'%'</TT> (percent) or <TT>'KM'</TT>
(kilometers) is specified after <TT>unit</TT> is looked up automatically
in table <TT>t006</TT>. The fields <TT>decan</TT> and <TT>andec</TT>
then determine how many decimal places are displayed. They both
contain a numeric value.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>decan</TT> specifies the number of zero-valued decimal values
to truncate. It removes trailing zeros; it will never truncate
nonzero decimal places. It also never increases the number of
decimal places displayed. For example, if the value is <TT>12.30</TT>
and <TT>decan</TT> is <TT>1</TT>, the output will be <TT>12.3</TT>.
If the value is <TT>12.34</TT> and <TT>decan</TT> is <TT>1</TT>,
the output will be <TT>12.34</TT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT>andec</TT> appends zeros after <TT>decan</TT> has been applied.
The number of decimal digits displayed must be a multiple of <TT>andec</TT>.
If necessary, <TT>andec</TT> will pad the value on the right with
zeros. If <TT>andec</TT> is zero, no zeros are appended. For example,
if the value is <TT>12.34</TT> after <TT>decan</TT> has been applied,
and the value of <TT>andec</TT> is <TT>3</TT>, there must be 0,
3, 6, or 9 (or any other multiple of 3) decimal digits. The output
value will therefore be padded with zeros to become <TT>12.340</TT>.
If the value after <TT>decan</TT> is <TT>12.3456</TT>, the output
will be <TT>12.345600</TT>. If the value after <TT>decan</TT>
is <TT>12.30</TT> and <TT>andec</TT> is <TT>2</TT>, the output
will be <TT>12.30</TT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
Table 14.6 contains more examples of the effects of <TT>decan</TT>
and <TT>andec</TT> on the output format.<BR>
</BLOCKQUOTE>
<P>
<CENTER><B>Table 14.6 How the decan and andec Fields
from Table t006 Affect the Output Formatting of a Field</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>Input </B></CENTER></TD><TD WIDTH=96><CENTER><B>decan</B></CENTER>
</TD><TD WIDTH=96><CENTER><B>andec</B></CENTER></TD><TD WIDTH=96><CENTER><B>Result</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>30.1</TT></TD><TD WIDTH=96><CENTER><TT>1</TT></CENTER>
</TD><TD WIDTH=96><CENTER><TT>0</TT></CENTER></TD><TD WIDTH=96><TT>30.1</TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96>30.1</TD><TD WIDTH=96><CENTER>1</CENTER></TD>
<TD WIDTH=96><CENTER>2</CENTER></TD><TD WIDTH=96>30.10</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96>30.1</TD><TD WIDTH=96><CENTER>1</CENTER></TD>
<TD WIDTH=96><CENTER>3</CENTER></TD><TD WIDTH=96>30.100</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96>30.1234</TD><TD WIDTH=96><CENTER>1</CENTER></TD>
<TD WIDTH=96><CENTER>3</CENTER></TD><TD WIDTH=96>3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -