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: 

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

0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

0 Kudos

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?

Former Member
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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

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

Former Member
0 Kudos

Hi,

Did you check my response?

Thanks

Former Member
0 Kudos

Tomas,

It works!!!

I can see the result on my system.

Can you plz check again and let me know.

Regards,

Raj

0 Kudos

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

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.