View previous topic :: View next topic
|
Author |
Message |
neelesht
New User
Joined: 24 Jul 2006 Posts: 99 Location: Los Angeles
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
neelesht
New User
Joined: 24 Jul 2006 Posts: 99 Location: Los Angeles
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
You're welcome
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 |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
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.... |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
|