Click here to Skip to main content
13,557,416 members
Rate this:
Please Sign up or sign in to 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 15-Feb-13 2:44am
Updated 15-Feb-13 3:29am
algioethqnbalkerunb 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-BNY 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180515.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100