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: 

Number of decimals

Former Member
0 Kudos
97

Hi all,

My requirement is :

I have a CHAR field (FIELD1) which would have values like 23.45, 12.3456.........

Now I have another field (FIELD2) which has the number of decimal places I need to care about.

For example -

If FIELD2 has '1' and FIELD1 has a value of '23.45' then I should overwrite FIELD1 value with '23.4'.

Similarly if FIELD2 has '2' and FIELD1 has '12.3456' then I should overwrite FIELD1 value with '12.34'.

And mind you if have '2' in FIELD2 and if FIELD1 has '2' then I should overwrite FIELD1 value with '2.00'.

I will summarize it again,


FIELD2   FIELD1   FIELD1
-------------------------
1        23.45    23.4
2        12.3456  12.34
2        2        2.00
0        12.34    12  

Can someone help me with some logic to get around this issue. I would be more than happy to award full points.

Waiting for your replies...

7 REPLIES 7

suresh_datti
Active Contributor
0 Kudos
49

use this statetment..

write field1 to field1 decimals field2.

~Suresh

0 Kudos
49

Hi Suresh,

I tried your logic but I dont think it works in my case. As my value field is a CHAR field and I think the 'DECIMALS' would work only for the types 'I', 'P' or 'F'.

Let me know if you have any other logic or correct me if I was wrong above.

Thanks n waiting....

Former Member
0 Kudos
49

<b>Suresh Datti</b> is right the code is OK.

0 Kudos
49

Let me try it once again and get back...

0 Kudos
49

Nope for some reason it is not working for me. I dont know what I am doing wrong. Below is the code I tried


data : value1 like qcres-mvalue.
data : value2 type i.

value1 = 2347 / 100.

shift value1 left deleting leading space.

value2 = 1.

write value1 to value1 decimals value2.

write 😕 value1.

The output of the above code is '23.47'. But I am expecting an output of '23.4'.

Suresh or someone please let me know what I am doing wrong.

Thanks...

0 Kudos
49

Try the below code.

lchr => character field.

lc1, lc2 => character field.

ltn => number of decimal places required

in concatenation statement of lc2 you can use the maximum possible length of decimals(in my case I have taken it as 6.)

lchr = lp2.

shift lchr left deleting leading space.

split lchr at '.' into lc1 lc2.

concatenate lc2 '000000' into lc2.

if ltn = 0.

clear lc2.

lchr = lc1.

else.

write lc2+0(ltn) to lc2.

concatenate lc1 lc2 into lchr separated by '.'.

endif.

write : / lchr.

Former Member
0 Kudos
49

hi

DATA : value1 type p decimals 2.

data value2(13) type c. "like qcres-mvalue .

DATA : val(1) TYPE c,

len TYPE i.

value1 = 2300 / 47 .

value2 = value1.

condense: value2 , val.

split value2 at '.' into value2 val.

len = strlen( val ).

write:/ value1.

WRITE value1 DECIMALS len.

regards,

VIjay