4

I have a requirement to format a decimal number with comma as decimal separator and without any thousands separator (neither dot nor space).

I would not be myself if I did not want to do it as one liner, however I have not been able to find such a solution.

What I have right now as workaround is.

DATA: l_value TYPE p LENGTH 9 DECIMALS 2 VALUE '-3450354.25'.

DATA(l_formatted_value) = |{ l_value SIGN = LEFTSPACE COUNTRY = 'DE ' }|.
REPLACE ALL OCCURENCES OF '.' IN l_formatted_value WITH ''.
WRITE: l_formatted_value.

I tried to do it with NUMBER = RAW option but then I do not seem to find a simple way to specify the decimal separator as comma.

DATA(l_formatted_value) = |{ l_value SIGN = LEFTSPACE NUMBER = RAW }|.
REPLACE '.' IN l_formatted_value WITH ','.

How can I achieve it as one liner? I would like to avoid using WRITE as well.

1 Answer 1

4

Nice challenge, here is a one-liner I found that works with your data definition. I tried to make it generic for any number of decimals but that turned out to be pretty difficult. Note that it will not always show two digits after the decimal character, e.g. 12.50 will show as 12,5.

|{ trunc( l_value ) },{  trunc( frac( l_value ) * 100  ) }|

I am curious what other people find.

5
  • Nice solution. Only one remark, you need to put abs function for the fraction part, if not there will be - after the comma for negative numbers. To get the zero at the end I changed it to |{ trunc( l_value ) },{ abs( trunc( frac( l_value ) * 100 ) ) WIDTH = 2 PAD = '0' }|. I will need only two places after the comma but I guess WIDTH option can be parametrised.
    – Jagger
    Nov 4, 2015 at 6:38
  • Good catch on the abs, I had it in there at some point since your original value was negative. Did not know about the formatting you added, so many new things to discover. Nov 4, 2015 at 8:07
  • Hi, what ABAP version is that? I can't compile the code. Nov 4, 2015 at 14:49
  • 1
    @NelsonMiranda I think it's 7.40. Nov 4, 2015 at 16:20
  • @NelsonMiranda If it comes to the formatting stuff between sings | it is before 7.40, it works for example with 7.11. If it comes to the declaration of variable l_formatted_value in my original post it is 7.40 indeed.
    – Jagger
    Nov 5, 2015 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.