Search code examples
localecurrencyabapdynpro

Group digits in currency and remove leading zeroes


I want to know how to do

  1. digit grouping

when I have value for money for example 3000000 ( 3million) i want to print 3.000.000 on the screen (there is a dot every three character from the last character)

  1. Remove zeroes in front of value

when I select a value from table and print it on the screen, the value get padded with zeroes automatically: e.g. 129 becomes 0000129


Solution

  • The WRITE statement allows you to specify a currency. Example:

    DATA price TYPE p DECIMALS 2.
    price = '3000000'.
    WRITE: / price CURRENCY 'USD'.
    

    Note that this does not interpret the number itself, but just adds commas and dots at certain positions depending on the currency you specify. So in the event you have an integer with the value of 3000000 and you write it with currency USD the result will be 30.000,00.

    I suggest you read the F1 help information on the WRITE statement, because there are a lot more options besides this one.

    --

    Removing leading zeroes is done by using a conversion routine. The CONVERSION_EXIT_ALPHA_INPUT will add leading zeroes and CONVERSION_EXIT_ALPHA_OUTPUT will remove them. It is possible to add these routines to a Domain in the dictionary, so the conversion will be done automatically. For example the MATNR type:

    DATA matnr TYPE matnr.
    matnr = '0000129'.
    WRITE: / matnr.
    

    This will output 129 because the Domain MATNR has a conversion routine specified.

    In the case of a type which does not have this, for example:

    DATA value(7) TYPE n.
    value = '0000129'.
    WRITE: / value.
    

    The output will be 0000129. You can call the CONVERSION_EXIT_ALPHA_OUTPUT routine to achieve the output without leading zeroes:

    DATA value(7) TYPE n.
    value = '0000129'.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = value
      IMPORTING
        output = value.
    WRITE: / value.