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: 

Fighting with decimals.

Former Member
0 Kudos

I'm doing something like

DATA var1 TYPE p DECIMALS 2.

.

.

.

var1 = 364 / 365.

I expect to receive "0.99" but instead of I receive "1.00".

Can someone explain me a little, of how to work with decimals in ABAP?

Thanx in advance.

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I looks to be rounding to the nearest, if you need to be more precise, extend the decimal places.



report zrich_0001.

DATA var1 TYPE p DECIMALS <b>3</b>.


var1 = 364 / 365.

write:/ var1.




Here you will get .997

Regards,

Rich Heilman

5 REPLIES 5

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I looks to be rounding to the nearest, if you need to be more precise, extend the decimal places.



report zrich_0001.

DATA var1 TYPE p DECIMALS <b>3</b>.


var1 = 364 / 365.

write:/ var1.




Here you will get .997

Regards,

Rich Heilman

0 Kudos

If you need it to be 2 decimals places, then you can do something like this. There must be a better way, I can't seem to give you one at the moment.



report zrich_0001.

DATA var1 TYPE p DECIMALS 3.
data: xvar1(10) type c.
data: xvar2 type p decimals 2.


var1 = 364 / 365.

write var1 to xvar1.
shift xvar1 right deleting trailing space.
shift xvar1 right by 1 places.

xvar2 = xvar1.

write:/ xvar2.

REgards,

Rich Heilman

0 Kudos

The system will always want to round up. If you values where just a little different, of course it would show the decimal.



report zrich_0001.

DATA var1 TYPE p DECIMALS 2.

var1 = <b>360</b> / 365.

write:/ var1. " VALUE is .99

If you find these answers a little helpful, please award points accordingly, Thanks.

Regards,

Rich Heilman

0 Kudos

If you always want to round down the divsion, here is a better way. Not sure what I was thinking, maybe too much Thanksgiving Turkey.



report zrich_0001.

DATA var1 TYPE p DECIMALS 2.

var1 = ( floor( ( 364 / 365 ) * 100 ) ) / 100.

WRITE:/ var1.

Here we are doing the division, multpling by 100 for the decimal places(value now is 99.7) then round down using FLOOR(value now is 99.0, then dividing by 100 for the 2 decimals places(value now is .99

Regards,

Rich Heilman

0 Kudos

That's it, thanx.