SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Convert Test String to Currency Value



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Mon Feb 25, 2008 1:47 pm    Post subject: Convert Test String to Currency Value Reply with quote

Code:
report zexp_txt2curr no standard page heading.
***********************************************************
* Convert String to Currency
*
*   This is freeware. Please read the terms of use.
*   Written by Urs Rohner, Rohner IT Consulting & Engineering
*
*   Contact Author: Urs Rohner
*
*  Comments
*  When writing interface programs in Abap that have to read external
*  data from network or files, I often encountered the following
*  problem: There is no standard function within R/3 (or at least I did
*  not find one), that allows conversion of a text string into a
*  standard amount with a given currency.
*  Simply moving the character field into the amount field does not
*  work since the internal representation of amounts are dependent on
*  the currency customizing, i.e. the number of digits after the
*  decimal point.
*
*  The following FORM allows this:function
*  (amount[curr],currency[cuky],returncode[sbyt]) <---
*    f (amount[char],currency[char],language[char])
*
*  the return code will be set to a non-zero value in case of an
*  conversion error.
*
*
* History
*    Vers  Date                Own   Remark
*    ----  ------------------  ---   ----------------------------------
*    1.00  16th January  2001  rnr   Initial version
*    1.10  30th May      2001  rnr   Added tags and additional comments
*    1.20  12th October  2001  rnr   Error found in SAP's FRACT compute.
*
***********************************************************

* Type Declarations
  types:
    txp_rc like sy-subrc.

* Method: m_txt_2_amount>
* Convert text to amount with given currency
  define m_txt_2_amount>.
    perform m_txt_2_amount using &1 &2 'E' &3 &4 &5.
  end-of-definition.

  form m_txt_2_amount using
    value(_t)         " amount as string
    value(_c)         " untyped currency
    value(_l)         " external value presentation
    _a                " amount
    _w                " currency
    _r like sy-subrc. " return code

  data:
    d_ type i, " decimals
    s_ type i, " shift
    f_(16) type p decimals 5,
    v_(16) type p decimals 5.

    clear _r.
    if not _c is initial.
      " try to find currency
      select single waers from tcurc into _w where waers eq _c.
      if sy-subrc is initial.
        " get number of decimals
        select single currdec from tcurx into d_ where currkey eq _w.
        if sy-subrc is initial.
          s_ = 2 - d_.
        else.
          d_ = 2. s_ = 0.
        endif.
        " try to convert string to float
        catch system-exceptions conversion_errors = 1.
        " set locale language _l.
          f_ = _t.
        " set locale language space.
        endcatch.
        if sy-subrc is initial.
          v_ = f_ * 10 ** d_.
          v_ = frac( v_ ).
          if v_ ne 0.
            _r = 5. " too much digits after decimal point
          else.
            catch system-exceptions conversion_errors = 1.
              _a = f_ / 10 ** s_.
            endcatch.
            if not sy-subrc is initial.
              _r = 4. " value is out of range
            endif.
          endif.
        else.
          _r = 3. " text is not a number
        endif.
      else.
        _r = 2. " unknown currency code
      endif.
    else.
      _r = 1. " currency not defined
    endif.
  endform.


* Globals
  data:
    r type txp_rc,
    c like bkpf-waers,
    v like bseg-wrbtr.

* Selection Screen
  parameters:
    t(20) type c           default '100',
    w     like coep-twaer  default 'BEF'.


* P A I ( )
  start-of-selection.
    m_txt_2_amount>  t w v c r.

    if r is initial.
      set locale language 'D'.
      write: v currency c, c.
    else.
      write: 'Error'(001), r.
    endif.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Conversion All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.