views:

365

answers:

1

here in switzerland our currency is francs and the smallest coin is 5 centimes which is 0.05 francs. what is the best way to round amounts to be payable with our money using the programming language ABAP in a SAP R/3 system?

examples:

" 4.48 should round to 4.50
" 2746.24 should round to 2746.25
+1  A: 
REPORT  zwvtest.

PARAMETERS: p_in  TYPE dec11_4,
            p_out TYPE dec11_4.

DATA: l_fraction    TYPE dec11_4,
      l_upper       TYPE dec11_4,
      l_delta_upper TYPE dec11_4,
      l_lower       TYPE dec11_4,
      l_delta_lower TYPE dec11_4.

AT SELECTION-SCREEN.
  l_fraction = FRAC( p_in * 10 ) / 10.
  l_upper = CEIL( l_fraction * 20 ) / 20.
  l_delta_upper = l_upper - l_fraction.
  l_lower = FLOOR( l_fraction * 20 ) / 20.
  l_delta_lower = l_fraction - l_lower.
  IF l_delta_lower < l_delta_upper.
    p_out = p_in - l_delta_lower.
  ELSE.
    p_out = p_in + l_delta_upper.
  ENDIF.

...if you want it rounded up or down depending on what's the closest value.

vwegert
thank you, but is this the only way? I thought that this kind of problem would be a common problem in the SAP world and there must be a less manually way... I have found the ROUND function-module but this didn't work.
flurin
If there is one, I haven't found it either. :-)
vwegert
ok, then I will go with your solution. ;)
flurin