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

String to decimal conversion in cobol


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

New User


Joined: 25 Jun 2007
Posts: 40
Location: chennai

PostPosted: Thu Sep 13, 2007 10:50 am
Reply with quote

I would like to know in cobol how we can move a string variable value (1199.99) to decimal variable 9(4)v9(2).
We pass this to commarea as a string and then we need to move it to the integer .
looking forward for a solution ..
Back to top
View user's profile Send private message
kbmkris

Active User


Joined: 24 Jun 2006
Posts: 101

PostPosted: Thu Sep 13, 2007 11:27 am
Reply with quote

Hi,

It is sufficient if you just move the string to a numeric variable.

Code:

77 WS-A         PIC X(5) VALUE '119.94'.
77 WS-B         PIC 9(3)V9(2) VALUE ZEROS.
....
....
MOVE WS-A TO WS-B.
...
...



As long as your string variable contains only numeric, you can move that directly. But if the length of the number in the string doesn't match with that of the numeric variable then truncation happens when the destination variable is of small length.

Please correct me if I am wrong.
Back to top
View user's profile Send private message
kbmkris

Active User


Joined: 24 Jun 2006
Posts: 101

PostPosted: Thu Sep 13, 2007 11:39 am
Reply with quote

Hi,

I apologize that the above one is not working properly when the number contains decimals.
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Thu Sep 13, 2007 12:17 pm
Reply with quote

FUNCTION NUMVAL will do what you are trying to achieve here..
Back to top
View user's profile Send private message
amrita.chatterjee

New User


Joined: 27 Apr 2006
Posts: 48
Location: Bangalore, India

PostPosted: Thu Sep 13, 2007 2:33 pm
Reply with quote

Another solution, u just split the total number into two parts. One integer and another decimal, & then move it to numeric part.

Steps: Use DELIMITED BY . into STR1 & STR2.

So if the string is 1234.55 then STR1 will contain 1234
STR2 will contain 55

Then define variable like
STR1 to NUM1(9(04)
STR2 to NUM2(9(02)

then u can merge it. May be this a very large process.

If i'm wrong then please correct me.
Back to top
View user's profile Send private message
sandeep1dimri

New User


Joined: 30 Oct 2006
Posts: 76

PostPosted: Thu Sep 13, 2007 3:38 pm
Reply with quote

Hi ,

you can use this approach also

suppose var1= ' 89.89'
replace the spaces of var1 by zeros

Code:
Inspect var1 replace all ' ' by 0

then
move Var1 to variable 9(4)v9(2)

Thanks
sandeep
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 Sep 13, 2007 8:07 pm
Reply with quote

Hello,

If you unstring the field with the actual '.' into 2 PIC X variables that are redefined as zoned decimal, it makes things very easy when the "pennies" part of the amount is defined as v99. Simply adding the 2 zoned-decimal pieces gets the proper value into a packed or zoned decimal field with an implied decimal.
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Thu Sep 13, 2007 8:19 pm
Reply with quote

Ignoring the sign and decimal point alignment,......
E x(??) value '(1199.99)'
N 9(??)
I comp
J comp

move length of E ro I
move length of N to J
perform varying I by -1 until I = zero
if E(I:1) numeric
move E(I:1) to N(J:1)
subtract 1 from J
end-if
end-perform
move zeros to N(1:J)
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 Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top