ABAP程序中的运行时使用引用变量和字段符号

时间:2014-10-22 23:23:43

标签: sap abap

我正在尝试遵循SAP Press book ABAP Basics中的代码。我正在使用与本书完全相同的代码,无法理解为什么它失败了。当我执行程序时,我遇到了运行时错误。我试图调试代码,发现我的代码在l_total_amount计算中的ELSE部分失败了。请告诉我我做错了什么?

An exception has occurred which is explained in more detail below. The exception, which is assigned to class 'CX_SY_CONVERSION_NO_NUMBER' was not caught . 

l_total_amount = <l_credit_amount> + l_vat_amount. 代码在此行失败

REPORT ZGULLA_SALES_ORDER_DYNAMIC.

PARAMETERS:
*Article Data
  p_ano(10) TYPE n OBLIGATORY,
  p_aname(40) TYPE c OBLIGATORY,
  p_aprice TYPE p DECIMALS 2 OBLIGATORY,
  p_curr TYPE currencysap OBLIGATORY DEFAULT 'EUR',
  p_aquant TYPE i OBLIGATORY DEFAULT 1,

*Tax

  p_tax TYPE p DECIMALS 2 DEFAULT '16' OBLIGATORY,

*Terms of payment

  p_cash TYPE c RADIOBUTTON GROUP 0001 DEFAULT 'X',
  p_credit TYPE c RADIOBUTTON GROUP 0001,
  p_months TYPE i OBLIGATORY DEFAULT '24'.

CONSTANTS:
*Interest per year in percent
  con_annual_interest TYPE p DECIMALS 2 VALUE '6.75'.

DATA:
*Temporary data
  l_net_amount TYPE p DECIMALS 2,
  l_tax_factor TYPE f,
*l_credit_amount TYPE p DECIMALS 2,
*l_monthly_interest_factor TYPE f,

*Result data
* l_monthly_vat_amount TYPE p DECIMALS 2,
* l_monthly_amount TYPE p DECIMALS 2,
  l_vat_amount TYPE p DECIMALS 2,
  l_total_amount LIKE l_net_amount,
* * Dynamic variables
  l_rda_credit_amount TYPE REF TO currency,
  l_rda_monthly_interest_factor TYPE REF TO f,
  l_rda_monthly_vat_amount TYPE REF TO currency,
  l_rda_monthly_amount TYPE REF TO currency.

FIELD-SYMBOLS:
* Access to reference variables
  <l_credit_amount> TYPE  any,
  <l_monthly_interest_factor> TYPE f,
  <l_monthly_vat_amount> TYPE any,
  <l_monthly_amount> TYPE currency.

* Temporary calculations
  l_net_amount = p_aprice * p_aquant.
  l_tax_factor = p_tax / 100.

* Write Article information to screen

WRITE: /, / 'Article information',
       / 'Article number: ', 30 p_ano,
       / 'Article name: ', 30 p_aname,
       / 'Article net price: ', 30 p_aprice, p_curr,
       / 'Quantity: ', 30 p_aquant.

*  Write conditions to screen

WRITE: /, / 'Conditions',
 / 'Tax rate: ', 30 p_tax,
 / 'Quantity: ', 30 p_aquant.

WRITE: /, / 'Result'.

IF p_cash = 'X'.
* Calculate cash results
  l_vat_amount = l_net_amount * l_tax_factor.
  l_total_amount = l_net_amount + l_vat_amount.

*   Write results to screen
  WRITE: / 'Total VAT amount: ', 30 l_vat_amount, p_curr,
         / 'Total amount: ', 30 l_total_amount, p_curr.
ELSE.

*   Calculate interest Results

  CREATE DATA l_rda_monthly_interest_factor.
  ASSIGN l_rda_monthly_interest_factor->* to <l_monthly_interest_factor>.
  CREATE DATA l_rda_credit_amount.
  ASSIGN l_rda_credit_amount->* to <l_credit_amount>.
  CREATE DATA l_rda_monthly_vat_amount.
  ASSIGN l_rda_monthly_vat_amount->* to <l_monthly_vat_amount>.
  CREATE DATA l_rda_monthly_amount.
  ASSIGN l_rda_monthly_amount->* to <l_monthly_amount>.

  <l_monthly_interest_factor> = con_annual_interest / 100 / 12.
  <l_credit_amount> = l_net_amount + l_net_amount * <l_monthly_interest_factor>
* p_months.
  l_vat_amount = <l_credit_amount> * l_tax_factor.

  l_total_amount = <l_credit_amount> + l_vat_amount.

  <l_monthly_vat_amount> = l_vat_amount / p_months.
  <l_monthly_amount> = l_total_amount / p_months.

*        Write results to screen
  WRITE: / 'Month: ', 30 p_months,
         / 'Monthly VAT amount: ', 30 <l_monthly_vat_amount>, p_curr,
         / 'Monthly amount: ', 30 <l_monthly_amount>, p_curr,
         / '(VAT amount: ', 30 l_vat_amount, p_curr, ')',
         / '(Total  amount: ', 30 l_total_amount, p_curr, ')'.
ENDIF.

2 个答案:

答案 0 :(得分:2)

您正在使用文本字段CURRENCY作为金额。因此,你得到一个短暂的转储。只需将类型更改为值类型,一切都应该没问题。我已将其更改为DEC23_2,一切顺利。

CURRENCY类型用于保存有关欧元,美元,英镑等货币的信息,并且是文本类型。

CURRENCY data type

答案 1 :(得分:1)

我猜问题是因为第114行的浮点范围。 只需转换为压缩十进制。添加以下代码

DATA : lv_monthly_interest TYPE p DECIMALS 2.
MOVE <l_monthly_interest_factor> TO lv_monthly_interest.
<l_credit_amount> = l_net_amount + l_net_amount * lv_monthly_interest * p_months.

希望这有帮助!!!