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: 

Problems with Packed numbers in formula mode.

Former Member
0 Kudos

Hello all,

I created a formula using the transaction-code VOFM. There was a moment in my code that I divided a number (var_1) by a quantity field (VBAP-VOLUM type QUANT DECIMALS 3)!

In runtime the formula used the DECIMALS part of the VOLUM field and because of that the result was incorrect!

var_1 = 100.

var_2 = var_1 / vbap-volum. “volum = 10.000 (PACKED)

  • Correct answer: var_2 = 10.

  • Incorrect answer: var_2 = 0,01. “this was the answer!!

If I do the same into an executable program, the result is OK.

The solution that I found was to multiply var_1 by 1000 before divide by the VOLUM field.

Anyone knows if this problem only occurs with formulas mode, or the problem could be the version that I am using (SAP - 4.0B)?

10x all and have a great day!

4 REPLIES 4

FredericGirod
Active Contributor
0 Kudos

Hi Felipe,

you will find this problem everywhere in SAP. The problem is that you play with the number of decimals. To avoid this problem use the same number of decimals in your formula code.

Rgd

Frédéric

mathias_lange3
Explorer
0 Kudos

Hi,

if you copy the value of vbap-volum into a helper field with:

WRITE vbap-volum to field decimals 3.

the value will be formatted to 10,000 instead of 10000.

As type for field try vbap-volum. This should solve your problem.

Kind regards

Mathias Lange

Former Member
0 Kudos

Hi,

It is in all versions (till 46C atleast) as others suggested and this is because the pricing program (function group V61A) is not marked for "Fixed point arithmetic". These formula routines are integrated into this function group and hence the logic that you wrote to multiply by 1000.

FYI, Documentation for "Fixed point arithmetic",

Fixed point arithmetic

If you mark this checkbox, all caluculations in the program will use fixed point arithmetic.

If you do not, packed numbers (ABAP/4 type P, Dictionary types CURR, DEC or QUAN) will be treated as integers when they are used in assignments, comparisons and calculations, irrespective of the number of decimal places defined. Intermediate results in arithmetic calculations will also be rounded to the next whole number. The number of decimal places defined is only taken into account when you output the answer using the WRITE statement.

Hope this helps..

Sri

brice_lagaly
Participant
0 Kudos

Hello all,

When you can't use "fixed point arthimetic" (ie: standard program) you have to calculate factor that will allow you to get the right value because packed numbers are considered as integers.

As an example: (consider that we don't know decimals number of each variable)

DATA : wl_dec1 type i,

wl_dec2 type i,

wl_dec3 type i,

wl_factor type i.

DESCRIBE FIELD klmeng decimals wl_dec1.

DESCRIBE FIELD zzntgew decimals wl_dec2.

DESCRIBE FIELD ntgew decimals wl_dec3.

wl_factor = 10 ** ( wl_dec1 + wl_dec2 - wl_dec3 ). " thisformula is different in case of division

ntgew = klmeng * zzntgew / wl_factor.

In case of division it should be :

wl_factor = 10 ** ( wl_dec1 - wl_dec2 + wl_dec3 ). "where wl_decx correspond to varx decimals

var1 = var2 / var3 * wl_factor.

As a solution you can also create a function module which uses "fixed point aritmetic" in which you send values as parameters and use wanted formula as single as you would write without any conversion factor.

Edited by: blsapsdn on Sep 7, 2011 11:45 AM