Application Development and Automation 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: 
Read only

Data type conversion from P(decimal) to type F(float)

0 Likes
4,407

Hi,

I have following problem:


DATA:
  l_value_p            TYPE p LENGTH 11 DECIMALS 8 value '33.33333333',
  l_value_f            TYPE f.

l_value_f = l_value_p.

The result is that l_value_f has value 3.3333333330000002E01. How come there is 2 at the end? I need value 3.3333333330000000E01. Seems to be some rounding issue. Does anybody know how to get rid of this rounding?

Thanks in advance,

Tomas

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,786

Hi,

The number of bytes in both cases do not match correctly.

Hence i believe a garbage value is inserted.

try the following code.

DATA:

l_value_p TYPE p LENGTH 11 DECIMALS 7 value '33.33333333', " value

l_value_f TYPE f.

l_value_f = l_value_p.

Write: l_value_f.

hope this helps,

Raj

10 REPLIES 10
Read only

Former Member
0 Likes
2,787

Hi,

The number of bytes in both cases do not match correctly.

Hence i believe a garbage value is inserted.

try the following code.

DATA:

l_value_p TYPE p LENGTH 11 DECIMALS 7 value '33.33333333', " value

l_value_f TYPE f.

l_value_f = l_value_p.

Write: l_value_f.

hope this helps,

Raj

Read only

0 Likes
2,786

Hi Raj,

thanks for your solution, in this way, the value is really converted precisely, but I simply cannot cut the last decimal number, I have to consider all 8 decimal numbers.

Any other idea?

Read only

Former Member
0 Likes
2,786

Hi Tomas,

Try this piece of code.

DATA:

l_value_p TYPE p LENGTH 11 DECIMALS 7 value '33.33333333', " value

l_char(11),

l_value_f TYPE f.

l_char = l_value_p.

l_value_f = l_char.

Write: l_value_f.

-Regards,

Raj

Edited by: Rajat Chaturvedi on Nov 27, 2008 5:30 PM

Read only

0 Likes
2,786

Hi Raj, this solution with l_char does not work for number with 8 decimal places either.

Read only

Former Member
0 Likes
2,786

Hi,

Execute this code.

DATA:

l_value_p TYPE p LENGTH 11 DECIMALS 8 value '33.33333333',

l_value_f TYPE float.

(I just changed type f to float)

l_value_f = l_value_p.

write l_value_f.

OUTPUT: 3.333333333000000E+01

Thanks

Nitesh

Read only

2,786

Hi Nitesh,

your solution surprised me - just changing "F" to "FLOAT".

Unfortunately, it does not work either. The value is printed correctly by WRITE but internally inside variable l_value_f it still holds value "3.3333333330000002E+01" and this is my problem.

I need the internal value of l_value_f to be 3.3333333330000000E+01.

To my understanding, this is problem cannot be solved.

This is because of type float - the conversion of any kind of numbers into float number is very dangerous and the result is unpredictable. I think we have to reconcider the usage of float in our application art the first place.

Thanks anyway, Tomas

Read only

Former Member
0 Likes
2,786

Hi,

Did you check my response?

Thanks

Read only

Former Member
0 Likes
2,786

Tomas,

It works!!!

I can see the result on my system.

Can you plz check again and let me know.

Regards,

Raj

Read only

0 Likes
2,786

Hi Raj, you are right, it works but only if the DECIMAL is 7, not if the DECIMAL is 8. And I cannot use DECIMAL 7 because I must not cut the last number.

That means, I need value

33,33333333

and not value

33,3333333

Thanks anyway, Tomas

Read only

2,786

As mentioned above, there is no solution for this because float is never so precise. Not all number can be described by float, for example this number.