Group digits in currency and remove leading zeroes

10,374

Solution 1

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.

Solution 2

Please also note that output conversion for numberlike types - triggered by the WRITE statement - is controlled by a property in the user master data. Decimal separator and digit grouping should be configured there.

You could check this in the user master transactions e.g. SU01 or SU01D.

Solution 3

For removing the zero padding use NO-ZERO statement. For the thousand separator I do not see any problem because it is a standard way ABAP prints values of type P. Here is a sample code.

REPORT  ZZZ.

DATA:
  g_n TYPE n LENGTH 10 VALUE '129',
  g_p TYPE p LENGTH 12 DECIMALS 2 VALUE '3000000'.

START-OF-SELECTION.
  WRITE /: g_n, g_p.
  WRITE /: g_n NO-ZERO, g_p.

This produces the output.

000000129
           3.000.000,00
      129
           3.000.000,00

Solution 4

For removing leading zeros, you can do the following:

data: lv_n type n length 10 value '129'.

shift lv_n left deleting leading '0'.
Share:
10,374
yukou
Author by

yukou

Updated on June 04, 2022

Comments

  • yukou
    yukou almost 2 years

    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