Reputation: 1
I want to write a cobol program when my input is 15.65778 then output should me 15.66 and When my input is 15.65000 then Output should be 15.65. Here rules is Basically two digits after the decimal point if its zero then output should be same otherwise input should be incremented by 0.1
Iam expecting a solution for this.
Upvotes: 0
Views: 233
Reputation: 4407
In this case, the standard alignment rules for a receiving item may be used to determine whether to increment the otherwise truncated value.
[See the note at the end for the explanation as to why the code was changed.]
Code:
data division.
working-storage section.
01 values-table.
03 pic 99v9(5) value 15.65000.
03 pic 99v9(5) value 15.65001.
03 pic 99v9(5) value 15.65778.
01 redefines values-table.
03 values-entry pic 99v9(5) occurs 3 indexed idx.
01 r pic vpp9(3) value 0. *> remainder
01 out-value pic 99.9(5).
01 out-result pic 99.99.
procedure division.
perform varying idx from 1 by 1
until idx > 3
move values-entry (idx) to out-value r
*> r now has the value beyond two decimal places
*> if that value is greater than zero
if r > 0
*> add 0.01 before truncating the result
compute out-result = values-entry (idx) + 0.01
else
*> otherwise truncate the result
move values-entry (idx) to out-result
end-if
display out-value " : " out-result
end-perform
goback
.
Output:
15.65000 : 15.65
15.65001 : 15.66
15.65778 : 15.66
Note: Previously, the intrinsic function REM
was used and, while the standard does not specify how to implement REM
, the implementor may use floating-point. If floating-point is used, the loss of precision during conversion may cause a non-zero value (xx001
) in the last three digits to become zero. Since the PICTURE
clause vpp9(3)
will preserve the exact value, the change was made.
Upvotes: 1
Reputation: 132
you can try this:
input pic 99v9(5)
temp pic 99v99
res-r pic 99.99
IF input(5:1) > 4 OR = ZERO
COMPUTE temp ROUNDED = input
move temp to res-r
ELSE
COMPUTE temp = input + 0.01
move temp to res-r
END-IF
input output:
15.65778 --> 15.66
15.65000 --> 15.65
15.65122 --> 15.66
Upvotes: 0