sapdev logo background
sapdev logo sapdev logo
Comments

Move minus sign(-) from end to start of field




SAP always stores negative numbers with the minus sign at the end of the value. This can cause problems when extracting out of SAP to external applications, for exampl excel expects the minus sign to be at the start of the value. Here is a simple bit of code to take the minus sign off the end and put it at the start. Please note variable being passed into this form needs to be of type char.


DATA: 	ld_neg_amt type p decimals 2,
	ld_amount(10) TYPE c.


****************************
*START-OF-SELECTION.
START-OF-SELECTION.

*Set value to minus 100
ld_neg_amt = ld_neg_amt - 100.

*store negative value in a char field 
ld_amount =  ld_neg_amt.

*display value with negative sign on the right
write:/(10) ld_amount.

*move negative sign to the left
PERFORM move_minus_sign CHANGING ld_amount.

*display value with negative sign on the left
write:/(10) ld_amount.


*&--------------------------------------------------------------*
*&      Form  move_minus_sign
*&-------------------------------------------------------------*
*       Move minus sign from end to begining of number value
*       i.e. from 100.00- to -100.00
*--------------------------------------------------------------*
*      <--P_AMOUNT  amount
*--------------------------------------------------------------*
FORM move_minus_sign  CHANGING p_amount.

* check if negative amount
  IF p_amount LT 0.
    SHIFT p_amount RIGHT DELETING TRAILING '-'.
    SHIFT p_amount LEFT DELETING LEADING ' '.
    CONCATENATE '-' p_amount INTO p_amount.
  ELSE.
    SHIFT p_amount LEFT DELETING LEADING ' '.
  ENDIF.
ENDFORM.                    " move_minus_sign



comments powered by Disqus