how to format a number to S9(5)V99 ascii in .net
Solution 1
What you have shown us here is
the PICTURE
clause portion of a COBOL data declaration.
COBOL data declarations are a bit odd and take some getting used to. Here is a link to an introductory tutorial on COBOL data declarations. This should get you started.
The PICture clause you have given in your question is defining a numeric item with the following characteristics:
-
S
- Leading sign -
9(5)
- 5 decimal digits -
V
- Implied decimal point -
99
- 2 digits after the implied decimal point
Basically, you are telling the COBOL compiler to define a numeric variable capable of holding
the values -99999.99 through +99999.99. Exactly how the compiler will fulfill this
request depends on the specific USAGE
clause. However, for numeric items containing a
fixed decimal position, the 'normal' USAGE is PACKED-DECIMAL
or COMP-3
(these are just
different names meaning the same thing). This link
provides some introductory information concerning the storage representation of packed decimal data.
Packed decimal data are useful for doing numeric computations where the number of decimal points must remain fixed.
Writing packed decimal data to a report or terminal does not work particularly well. You must
first convert it to a DISPLAY
able format. This involves MOVE
ing the packed decimal value to another
variable with a USAGE DISPLAY
attribute. Suppose your packed decimal variable was called
PACKED-DECIMAL-NBR
and was holding the value -2345.01. You could define a display variable
to hold it as:
01 DISPLAY-NBR PIC +++,++9.99.
then when it comes time to write/display the value contained in PACKED-DECIMAL-NBR
you would
do something like:
MOVE PACKED-DECIMAL-NBR TO DISPLAY-NBR
DISPLAY DISPLAY-NBR
The MOVE
converts the packed-decimal number to a character representation which you can
display in reports or on the terminal. The value -2,345.01
is displayed.
Solution 2
S in cobol is the definition of a signed numeric field (ie can be positive or negative). So for example S999 is a signed 3 digit numeric (binary). Problem is S is assumed, and in most cobol compilers, the way the sign is marked is by "overpunching" the last character with another character to signify plus or minus. This results, if you look at the field in an ascii browser, you will have a strange character. Ie. MINUS 500 would look something like this 50C or 50} depending on the underlying character and if it were a + or - that had overpunched.
Related videos on Youtube
user392973
Updated on July 05, 2022Comments
-
user392973 almost 2 years
I've been searching for s9(5)v99 but got different information and not really clear. Could someone shows how or the formula to convert. thanks
-
user392973 almost 14 yearsI want to upload a file to a web service. To their specification, the amount field is in format S9(5)V99
-
user392973 almost 14 yearsI want to upload a file to a web service. To their specification, the amount field is in format S9(5)V99. The amount field in our sql database is decimal field
-
NealB almost 14 years@user392973 @John Saunders. Based on your comment to John Saunders, it is possible that the USAGE type your client is looking for is not PACKED-DECIMAL but DISPLAY (better clarify that with your client). If USAGE is DISPLAY, then just allocate an 8 byte string and format the number as indicated in the answer John gave you. If the target environment is an IBM mainframe, the next bit to watch for are code page issues between EBCDIC/ASCII characters sets.
-
John Saunders almost 14 years@user: they should learn a language other than COBOL. S9(5)V99 is meaningless to anyone other than a COBOL programmer.
-
John Saunders almost 14 yearsIf these people want a web service to send them PACKED-DECIMAL or EBCDIC, then they need to wake up and smell the decade.
-
NealB almost 14 years@John Saunders. Agreed! There are still some people finding IBM mainframes running COBOL at the center of their universe. It is important to be aware of the kinds of issues you might be running into when working with them. Anybody that specifies a data interchange format based on COBOL picture clauses is not just a few decades behind the times - they are clearly lost.
-
Bill Woodger about 11 yearsWhy do you feel S999 is "binary"? It needn't be. S is "implied", not "assumed". USAGE DISPLAY fields are "overpunched", packed-decimal/comp-3 or binary/comp/comp-4/comp-5 are not, as they would not be "punched" in the first place. "50C" would be positive 503, "50}" is -500. If it were a "+" that had been overpunched, then it would be a positive number.
-
Dragonthoughts over 4 yearsPlease include an explanation of your answer, not just code.