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: 

No-Rounding

marcosfernando_dotta
Participant
0 Kudos

Hi,

Please, could somebody help me??

I would like to solve this aritmethic expression but without rounding. I found some commands like TRUNC, FRAC etc; but it doesn't help me. I didn't find any article in internet explanning it.

The result must be 4.14 instead of 4.40!

DATA: vl_total_frete(15) TYPE n ,

vl_icsf(15) TYPE p DECIMALS 2,

vl_aliq_cof(6) TYPE n .

WRITE '54.55' TO vl_total_frete.

WRITE '7.60' TO vl_aliq_cof .

*vl_icsf = ( ( vl_total_frete * vl_aliq_cof ) / 100 ).*

Thanks,

Marcos F. Dotta

Edited by: Marcos Dotta on Jan 27, 2011 11:44 AM

1 ACCEPTED SOLUTION

former_member1245113
Active Contributor
0 Kudos

HI

Check the below code

DATA: vl_total_frete(15) TYPE p decimals 2,
vl_icsf(15) TYPE p DECIMALS 3, " Change the decimals to 2 and check again
vl_aliq_cof(6) TYPE p decimals 2.

vl_total_frete = '54.55'.
vl_aliq_cof = '7.60'.
* Since type n accepts numbers after decimals but while calculating they are not taken into account
* It is merely acting as string/Char type while accepting the value but not at the time of Calculation
vl_icsf = ( ( vl_total_frete * vl_aliq_cof ) / 100 ).

Cheerz

Ram

5 REPLIES 5

0 Kudos

HI,

Hope this helps

DAtA : w_data1 like vl_total_frete,

w_data2 like vl_aliq_cof.

WRITE '54.55' TO vl_total_frete.

WRITE '7.60' TO vl_aliq_cof .

w_data1 = ABS(vl_total_frete).

w_data2 = ABS(vl_aliq_cof).

vl_icsf = ( ( w_data1 * w_data2 ) / 100 ).

  • Basically this w_data1 wil be equal to 55 & w_data2 = 8

*vl_icsf wil be equal to 4.40

Regards,

Akhila Bidare

0 Kudos

Hi Akhila,

But the result can not be 4.40 but 4.14... You code didn't work, sorry!!

The rounding must not happen!

Thanks,

Marcos F. Dotta

former_member1245113
Active Contributor
0 Kudos

HI

Check the below code

DATA: vl_total_frete(15) TYPE p decimals 2,
vl_icsf(15) TYPE p DECIMALS 3, " Change the decimals to 2 and check again
vl_aliq_cof(6) TYPE p decimals 2.

vl_total_frete = '54.55'.
vl_aliq_cof = '7.60'.
* Since type n accepts numbers after decimals but while calculating they are not taken into account
* It is merely acting as string/Char type while accepting the value but not at the time of Calculation
vl_icsf = ( ( vl_total_frete * vl_aliq_cof ) / 100 ).

Cheerz

Ram

Former Member
0 Kudos

Hi Marcos,

Firstly, you have to use type p and not type n when you deal with decimals.

Secondly, WRITE statement copies the number like a character and not a number.

If you want to do type-casting then you have to use MOVE.

E.G.

DATA: vl_total_frete(15) TYPE p DECIMALS 2 ,"Type p not n
vl_icsf(15) TYPE p DECIMALS 2,
vl_aliq_cof(6) TYPE p DECIMALS 2 ."Type p not n

MOVE '54.55' TO vl_total_frete. "Use Move instead of write
MOVE '7.60' TO vl_aliq_cof . "Use Move instead of write

vl_icsf = ( ( vl_total_frete * vl_aliq_cof ) / 100 ).

WRITE vl_icsf.

Regards,

Jovito

0 Kudos

Jovito's solution works correctly, however keep in mind that your calculations result is actually 4.1458, so what gets stored into vl_icsf will be 4.15, not 4.14. in essence it IS still being rounded, but you are now able to retain 2 decimal places.