Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to format a field that has 4 decimal places 2

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I need to format a field to have 4 decimal places.

I am using,

Code:
        $vQty      = sprintf("%0*d", 13, $data[7]);

it should be 9(09)V9(04)at least that is the format in Cobol.

I could do

$vQTY = $data[7]*10000;
$vQty = sprintf("%0*d", 13, $vQTY);

That would put the digits in the correct place, but it isn't really correct.
 
It has been decades I used COBOL, but if I remember correctly the
PICTURE 9(9)v9(4) format means that you have a number stored like this: "0034567890123", but actually represents "003456789.0123" which is the way you want it to print

So try this:
Code:
$data[7]="0034567890123";
$vQty = sprintf("%014.4f", $data[7]/10000);

Make sure $data[7] is initialized with the quotes, otherwise Perl will think that the leading zeros represent an octal number, and therefore fail to initialize $data[7] when it interprets the invalid octal digits of "8" and "9".

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top