Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Number format issue

Former Member
0 Kudos

Hi All,

I'm facing some problem in assigning the number format.

Currently, I'm comparing a Variable of Char type to a Variable of char type.

Example: Lv_char gt lv_char1.

In my profile, I have dot number format(567.89). Where as the Users have different number format in their profile (ex 567,89).

When lv_char is getting a value like (7.89), they are getting (7,89) ...in this case the report is giving dump saying cant interpret 7,89 as number.

I have suggested to one of the users to change the number format in thier profile. But today again another User got the same problem.

I cannot tell him again to change the number format bcoz he mentioned that he want 7,89 number format for other purpose.

Now, pls tell me how to change the comma format to dot format.

Thanks,

Priya

11 REPLIES 11

kesavadas_thekkillath
Active Contributor
0 Kudos

Hi,

The easiest way is to include the option in screen to mention the decimal and thousand separator. So that you can replace easily

If it is not possible then check if this helps, I wrote this some time back for an excel upload to determine the format and change it to internal format.

IF ls_split CO c_allowed_number. "C_ALLOWED_NUMBER VALUE ''0123456789.,''.

                  lv_value =  me->get_numeric_value( iv_number = ls_split ).

else.   "Shout

  lv_row = lv_data_tabix.

  lv_message = 'Invalid number format provided'(008).

  CONCATENATE lv_message '-' 'Row'(009) '-' lv_row INTO lv_message.

   MESSAGE e114 WITH lv_message RAISING e_file_structure.

   lv_error = abap_true.

   EXIT.

Endif.

METHOD get_numeric_value.

   DATA: lt_result_tab TYPE match_result_tab,

        ls_result TYPE match_result,

        lv_number TYPE string,

        lv_seperator(1) TYPE c,

        lv_before TYPE string,

        lv_after TYPE string,

        lv_differs TYPE c,

        lv_seperator_next(1) TYPE c,

        lv_count TYPE i.

  CHECK iv_number IS NOT INITIAL.

* Get the no. of seperator in the incoming value

* condense provided to support the SAP format 'Y'

  CONDENSE iv_number NO-GAPS.

  FIND ALL OCCURRENCES OF REGEX '[^0-9]' IN iv_number RESULTS lt_result_tab.

  lv_count = lines( lt_result_tab ).

  lv_number = iv_number.

  IF lv_count = 0.

    rv_result = iv_number.

    RETURN.

  ELSE.

* if the incoming format is somethig like 0.0 & 0,0 etc...return just 0.

    REPLACE ALL OCCURRENCES OF REGEX '[^0-9]' IN lv_number WITH ` `.

    CONDENSE lv_number NO-GAPS.

    IF lv_number = 0." If the value is zero

      rv_result = lv_number.

      RETURN.

    ENDIF.

  ENDIF.

  lv_number = iv_number.

* if there is only one seperator

* Here the value can fall in any of the SAP user settings format

* it might be a decimal or thousand seperator

* If the sap format is 12 34 56,80 then the thousand sep. is ' ' and decimal is ','

* and it falls in count = 1.

  IF lv_count = 1.

    READ TABLE lt_result_tab INTO ls_result INDEX 1.

    IF sy-subrc = 0.

      lv_seperator = iv_number+ls_result-offset(1).

* check if it is the thousand seperator as per user setting

* if true replace it to space

* else change it to '.'

      IF me->mv_thousand_seperator EQ lv_seperator.

        REPLACE lv_seperator IN iv_number WITH space.

        rv_result = iv_number.

        CONDENSE rv_result NO-GAPS.

      ELSE.

        REPLACE lv_seperator IN iv_number WITH '.'.

        rv_result = iv_number.

        CONDENSE rv_result NO-GAPS.

      ENDIF.

    ENDIF.

  ELSEIF lv_count > 1.

* More than one seperators

    LOOP AT lt_result_tab INTO ls_result.

* Get the first seperator

      IF sy-tabix = 1.

        lv_seperator = iv_number+ls_result-offset(1).

        CONTINUE.

      ELSE.

* get the next seperator

        lv_seperator_next = iv_number+ls_result-offset(1).

      ENDIF.

* If there is a difference then exit

      IF lv_seperator_next <> lv_seperator.

        lv_differs = abap_true.

        EXIT.

      ENDIF.

    ENDLOOP.

* If its is not differing then there is only one seperator used

* directly replace it with space and return.

    IF lv_differs = abap_false.

      REPLACE ALL OCCURRENCES OF REGEX '[^0-9]' IN iv_number WITH space.

      rv_result = iv_number.

      CONDENSE rv_result NO-GAPS.

    ELSE.

* Always replace the last seperator as '.' and replace other as space and return.

      READ TABLE lt_result_tab INTO ls_result INDEX lv_count.

      IF sy-subrc = 0.

        ls_result-offset = ls_result-offset + 1.

        lv_after = iv_number+ls_result-offset(*).

        lv_before = iv_number+0(ls_result-offset).

        REPLACE ALL OCCURRENCES OF REGEX '[^0-9]' IN lv_before WITH space.

        CONCATENATE lv_before '.' lv_after INTO rv_result.

        CONDENSE rv_result NO-GAPS.

      ENDIF.

    ENDIF.

  ENDIF.

D.

The variable mv_thousand_seperator,mv_decimal_seperator is set as below

METHOD set_number_format.

DATA:ls_usr01 TYPE usr01.

  CHECK mv_thousand_seperator IS INITIAL AND

        mv_decimal_seperator IS INITIAL AND

        mv_number_format IS INITIAL.                    " Then Silently Leave

  SELECT SINGLE * FROM usr01 INTO ls_usr01 WHERE bname = sy-uname.

  IF ls_usr01-dcpfm = ' '.

    mv_thousand_seperator = '.'.

    mv_decimal_seperator = ','.

    mv_number_format = '#.##0,00'.

  ELSEIF ls_usr01-dcpfm = 'X'.

    mv_thousand_seperator = ','.

    mv_decimal_seperator = '.'.

    mv_number_format = '#,##0.00'.

  ELSEIF ls_usr01-dcpfm = 'Y'.

    mv_thousand_seperator = ' '.

    mv_decimal_seperator = ','.

    mv_number_format = '# ##0,00'.

  ENDIF.

ENDMETHOD.

0 Kudos

Hello,

Any function module to convert comma separate to Dot separate?

Rgs,

Priya

0 Kudos

Hi Priya,

why do you need a function module?

     TRANSLATE my_var USING ',..,'.

should do ?

Regards,

Dirk

0 Kudos

There is no function module to convert , I have checked it. The external format can be of X Y and space. So you cannot always convert comma to dot( it cannot be fixed ). Just try copying the code which I have provided in to a routine. There is no change required. Only populate the mv_thousand_seperator from table usr01.

0 Kudos

Drik,

This way it wont work out.

In my profile, I have dot format.

If I implement the way you suggested then, suppose if I have number like 10,000.25 then it will convert this to 10.000,25 which actually should not.

Hence I cannot do it.

Anyways thanks for your suggestion.

0 Kudos

Kesavadas,

Thanks for your input.

I'm analyzing it, dont understand the first part of your code though.

But trying!!!

Thanks again.

Jelena
Active Contributor
0 Kudos

Obvious solution is to compare two numbers. When dealing with numeric types, user's format is irrelevant. Why are you comparing CHAR variables? Can you post a bigger code fragment?

In any case, it's always a good practice to wrap any type conversions in TRY...CATCH... with appropriate exception class to avoid short dumps.

0 Kudos

Hi Jelena,

Consider a situation where the data comes from a file where the amount field has ',' for decimal indicator.Now, when reading it in the program, we cannot do any manipulation on the field till the format is converted to internal. (ie) '.' as decimal. In the file it could be the other way too,  '.' for decimal or  space for thousand. ( ' ','X' or 'Y' of external sap formats )

We could "assume" that the external file number format will be same as external sap number format and then convert it internally. In my case we got the user confirmation that it will be maintained in file with SAP external format.

Jelena
Active Contributor
0 Kudos

To be able to convert the number correctly, we'd need to know either the user's decimal notation or how many decimals are in the number (and this should be fixed number). Otherwise if in the file we have '1,000' how would the program know if it's one thousand or one with 3 decimals?

If it's known how many decimals are in the number then it's a simple matter of "scaning" the number from the right to see whether it has a dot or a comma as a separator.

If the users are uploading the file online, maybe it's possible to read the PCs local settings (registry?) to find their decimal notation. Or maybe a simple macro could be provided to them that would create a file with the consistent format. There are many creative options, although none is perfect, unfortunately.

When it comes to the file upload, the format just needs to be consistent with what was programmed for. It's not an "assumption" but rather an "agreement".

Former Member
0 Kudos

Hi Priya,

Have you tried

write <source> to <destination>.

Here <destination> will be a character filed.

I hope, it will certainly work.

edgar_nagasaki
Contributor
0 Kudos

Dear Priya,

Think the easiest way to solve your problem is to first define the notation to be used what can be done through SET COUNTRY statement before your variables comparison. If, for instance, you decided to use comma as decimal separator you would need to choose a country that corresponds to the same (eg. US):

SET COUNTRY 'US'.

Regards,

Edgar