Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert char field value to decimal

ram_sahoo
Participant
0 Kudos
2,737

Hi Experts,

I have requirement to convert from char field value to decimal.

Example -

char type field value = 314500

i want to convert it to decimal with 2 mean my expected result should - 3145.00

I tried to use pack syntax but in side class method pack will not work, anyone can suggest.

Thanks

1 ACCEPTED SOLUTION

kartikrajag
Participant
0 Kudos
1,622

see whether it makes sense to your scenario? attached image has output in console.

data: lv_value_char type char10 value '314500', 
      lv_value_dec type p DECIMALS 2,
clear: lv_value_dec.
lv_value_dec = lv_value_char / 100.

5 REPLIES 5

Abinathsiva
Active Contributor
1,622

Hi,

Try with FM - WSAF_CONV_STRING_TO_DECIMAL this will solve the requirement...

ram_sahoo
Participant
0 Kudos
1,622

Hi Abinath,

I tried FM- WSAF_CONV_STRING_TO_DECIMAL but confused with parameter values. Can you please help on this.

Thanks

Sandra_Rossi
Active Contributor
0 Kudos
1,622

PACK was useful for handling currency amounts, because SAP stores the currency amounts in the database with a floating decimal point (i.e. it ignores the number of decimals of the column).

If you want to interpret 314500 (database value) as an amount in a currency with 2 decimals like EUR or USD (and many others), you may use CL_ABAP_DECFLOAT:

DATA: curr       TYPE p LENGTH 9 DECIMALS 2,
      decfloat34 TYPE decfloat34.

decfloat34 = 314500.
cl_abap_decfloat=>convert_decfloat_to_curr(
      EXPORTING
        amount_decfloat = decfloat34
        cuky            = 'EUR'
      IMPORTING
        amount_curr     = curr ).
ASSERT curr = 3145.

kartikrajag
Participant
0 Kudos
1,623

see whether it makes sense to your scenario? attached image has output in console.

data: lv_value_char type char10 value '314500', 
      lv_value_dec type p DECIMALS 2,
clear: lv_value_dec.
lv_value_dec = lv_value_char / 100.

0 Kudos
1,622

if you set the value of lv_value_char to 178862‬00 ,

then you will face the error

can you check.