简体   繁体   中英

CX_SY_CONVERSION_NO_NUMBER during a calculation

I am trying following code from the SAP Press book ABAP Basics. I am using exactly the same code from the the book and unable to understand why it is failing. While I am executing the program I am getting following Runtime Error. I have tried to debug the code and found that my code is failing in ELSE section at l_total_amount calculation. Could please let me know what I am doing wrong?

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 .

code fails at this line : 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. "<=============== FAILS

  <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.

You are using text field CURRENCY for amounts. Therefore you get a short dump. Just change the type to a value type and everything should be ok. I have changed it to DEC23_2 and everything ran smoothly.

The CURRENCY type is used to hold information about the currency like EUR, USD, GBP, etc. and is a text type.

货币数据类型

I guess issue is because of floating point range at Line no 114. Just convert to packed decimal.By adding the below code

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.

Hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM