IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

What is the syntax for Round-Up in Cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
neelesht

New User


Joined: 24 Jul 2006
Posts: 99
Location: Los Angeles

PostPosted: Wed Apr 18, 2007 9:33 pm
Reply with quote

Hi All,

I want to round-up one of the variable.
How do I do this ?

Example
Var(A) signed.

if its value is 0.22 I want to round it to 1 and also if its 0.99 then also round it to 1.
if its 1.01 then it should be rounded to 2.
Please give me the exact syntax.

Thanks
-Neelesh
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Apr 18, 2007 10:25 pm
Reply with quote

Hello,

One way might be to do your calculation resulting in a field with 2 implied decimals (05 MY-AMT PIC S9(7)V99 COMP-3). After your calculation, compute a new field (05 NEW-AMT PIC S9(7) COMP-3) with
Code:
COMPUTE NEW-AMT = MY-AMT + .99


This should generate the answer you want.
Back to top
View user's profile Send private message
neelesht

New User


Joined: 24 Jul 2006
Posts: 99
Location: Los Angeles

PostPosted: Wed Apr 18, 2007 10:43 pm
Reply with quote

Hi Dick,

thanks alot.

Is there any thing called ROUND-UP in Cobol, since I dont want to define any working storage variable.

Thanks
-Neelesh
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Apr 18, 2007 11:22 pm
Reply with quote

You're welcome icon_smile.gif

COBOL has a ROUNDED option, but it 1/2 adjusts by .5.

If you do a computation with fields that have decimal places, you will have decimal places in the result. If you want to round up to the next full digit and have the answer be 2, i believe you will need to create a result with no decimal places - otherwise you would at best get 2.00 for a result.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Thu Apr 19, 2007 1:39 am
Reply with quote

neelesht wrote:
if its value is 0.22 I want to round it to 1 and also if its 0.99 then also round it to 1.
if its 1.01 then it should be rounded to 2.
In very few words, "that ain't rounding"!
The three ways to "round" are:
Truncate - round down
Round - known as half round
And round up - ok, the word "round" is in there....

Code:
01 filler.
  05 NumberToRoundUp pic s9(5)v99.
  05 NumberRoundedUp pic s9(5)v99.
  05 VAR pic s9(5)v99.
  05 filler redefines VAR.
    10 filler pic x(5).
    10 CNT pic x(2).

move NumberToRoundUp to VAR
if cnt = '00'
   continue
else
   add 1 to VAR
   move zero to CNT
end-if
move VAR to NumberRoundedUp

Or
Code:
01 filler.
  05 NumberToRoundUp pic s9(5)v99.
  05 NumberRoundedUp pic s9(5)v99.
  05 VAR pic s9(5).

move NumberToRoundUp to VAR
if VAR = NumberToRoundUp
   continue
else
   add 1 to VAR
   move VAR to NumberRoundedUp
end-if

Or you might play with integer functions.... icon_lol.gif
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Apr 19, 2007 3:41 am
Reply with quote

Hello,

The required result is quite common at places i've supported.

Let's say that there is a container system that is of fixed size for loading on trucks and trains. They have been designed to maximize the product load for the transport type. These containers may be loaded with lots of different product. When an order is received that takes 5.02 containers, 6 must be allocated to handle that order.

In those cases the "result" was "rounded" by .99 to ensure everything was accounted for. Don't know if this is what Neelesh is doing, but we used this at several places.

Neelesh - can you live with 2.00 for an answer?

Unless there is some "real" problem with creating a new field with no decimal places, i'd recommend definng a new result field with no decimals.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top