Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to?

Cobol has an input mask format similar to the following:
Using integer 345
picture "99999" yields 00345
picture "zzzz9" yields 345
or even "zzzVzz" yields 3.45

"9zzzVzz" invalid (9 to the left of z).

"$zzVzz" floating dollar $3.45

integer 1234567

picture "$zzzTzzzVzz"

results in $12,345.67

V is dot in non metric countries, and
Some software uses the T to indicate comma. T is comma , in USA, and dot . in metric countries.

Is there already a routine that does this? something like

picformat(Result, Picture,integer),

There is a rule for sign (when integer was negative). S
$SzzzTzzzVzz or $zzzTzzzVzzS
$-12,345.67 $12,345.67-
Posted
Updated 15-Feb-13 3:29am
v2
Comments
fjdiewornncalwe 15-Feb-13 9:28am    
The term you are using is incorrect. What you are looking for is a "Mask", not a "Picture".
nv3 15-Feb-13 10:08am    
Marcus, I think he is referring to the COBOL picture clause (PIC). Yes, it was really called "PIC"!.
Chris Reynolds (UK) 15-Feb-13 9:58am    
The nearest you'll get is sprintf for formatting output and sscanf for parsing an input string. You could probably write a routine to convert standard COBOL picture formats to a sprintf format specifier, the commas will be a problem though.

These features of COBOL are built into the compiler, and are not generally available in other languages (with some exceptions: PL/I, ADA).

You flagged "C" as your language of interest. These features are not built into the C language, but you might be able to find a library somewhere that has implemented similar functionality.
 
Share this answer
 
Adding commas using sprintf(), was no challenge. I have tested it with positive and negative numbers between one digit and 16 digits.

I need to remove the debugging code and post it here in the next day or two.

I have almost completed the formating using the Cobol Picture clause. The rules allow for truncation.

My first step was to edit the picture clause to insure the format was correct and not illegal.

The second step is to actually doing the formatting.

This will come in the next day or so as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900