{"id":1048,"date":"2014-09-03T09:52:41","date_gmt":"2014-09-03T08:52:41","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1048"},"modified":"2014-09-03T09:55:26","modified_gmt":"2014-09-03T08:55:26","slug":"abap-convert-numbers-on-input-and-output","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1048","title":{"rendered":"ABAP &#8211; Convert numbers on input and output"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>I&#8217;m affraid there might be a standard function module already available to achieve the same what I&#8217;m going to write myself, but I believe it is good to know that the number format can be different when different user settings is used in SAP.<br \/>\nIf you forget about this during your development you might start fighting serious problems in the future.<br \/>\nTherefore I&#8217;ve written two short code snippets for numbers conversion for <em>INPUT<\/em> (save to database) and <em>OUTPUT<\/em> (display to the user)<!--more--><\/p>\n<pre lang=\"abap\">DATA: l_value(8) type c.\r\n\r\nCLASS lcl_utils DEFINITION.\r\n  PUBLIC SECTION.\r\n    CLASS-METHODS:\r\n      class_constructor,\r\n      convert_number_input\r\n        CHANGING c_value TYPE any,\r\n      convert_number_output\r\n        IMPORTING\r\n          i_length   TYPE i\r\n          i_decimals TYPE i\r\n        CHANGING c_value TYPE any.\r\n\r\n  PRIVATE SECTION.\r\n    CLASS-DATA:\r\n      m_dcpfm TYPE xudcpfm.\r\nENDCLASS.                    \"lcl_utils DEFINITION\r\n\r\nCLASS lcl_utils IMPLEMENTATION.\r\n  METHOD class_constructor.\r\n*   Read number format from user settings\r\n    SELECT SINGLE dcpfm\r\n      INTO m_dcpfm\r\n      FROM usr01\r\n      WHERE bname = sy-uname.\r\n  ENDMETHOD.\r\n\r\n  METHOD convert_number_input.\r\n    CASE m_dcpfm.\r\n      WHEN ''.\r\n        REPLACE ALL OCCURRENCES OF '.' IN c_value WITH ''.\r\n        TRANSLATE c_value USING ',.'.\r\n      WHEN 'X'.\r\n        REPLACE ALL OCCURRENCES OF ',' IN c_value WITH ''.\r\n      WHEN 'Y'.\r\n        CONDENSE c_value NO-GAPS.\r\n        TRANSLATE c_value USING ',.' .\r\n    ENDCASE.\r\n  ENDMETHOD. \r\n\r\n  METHOD convert_number_output.\r\n    DATA:\r\n      lr_data TYPE REF TO data,\r\n      lr_var_description TYPE REF TO cl_abap_elemdescr.\r\n\r\n    FIELD-SYMBOLS:\r\n      &lt;fs_data&gt; TYPE ANY.\r\n\r\n    lr_var_description = cl_abap_elemdescr=&gt;get_p(\r\n      p_length   = i_length\r\n      p_decimals = i_decimals\r\n    ).\r\n\r\n    CREATE DATA lr_data TYPE HANDLE lr_var_description.    \r\n    CHECK lr_data IS BOUND.\r\n\r\n    ASSIGN lr_data-&gt;* TO &lt;fs_data&gt;.\r\n    &lt;fs_data&gt; = c_value.\r\n    WRITE &lt;fs_data&gt; to c_value.\r\n  ENDMETHOD.                    \"CONVERT_NUMBER_OUTPUT\r\nENDCLASS.                    \"lcl_utils IMPLEMENTATION\r\nSTART-OF-SELECTION.\r\n  l_value = '4.5689'.\r\n  WRITE: 'Original: ', l_value.\r\n  lcl_utils=&gt;convert_number_output(\r\n    EXPORTING\r\n      i_length   = 8\r\n      i_decimals = 2\r\n    CHANGING\r\n      c_value    = l_value\r\n  ).\r\n  WRITE:\/ 'Output with length=8 decimals=2: ', l_value.\r\n\r\n  lcl_utils=&gt;convert_number_input(\r\n    CHANGING\r\n      c_value = l_value\r\n  ).\r\n  WRITE:\/ 'Input: ', l_value.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m affraid there might be a standard function module already available to achieve the same what I&#8217;m going to write myself, but I believe it is good to know that the number format can be different when different user settings &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=1048\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - Convert numbers on input and output","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,15,9,38,5],"tags":[293,279,29,317,318,221,278],"class_list":["post-1048","post","type-post","status-publish","format-standard","hentry","category-abap","category-algorithms","category-development","category-software","category-tools","tag-convert","tag-dcpfm","tag-dynamic","tag-number-input","tag-number-output","tag-oo-abap","tag-usr01"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-gU","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1048"}],"version-history":[{"count":3,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1048\/revisions"}],"predecessor-version":[{"id":1051,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1048\/revisions\/1051"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}