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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl sprinf zero padding format issue.

Status
Not open for further replies.

jgarcia2002

Programmer
Mar 20, 2002
2
US
Hi All

Need a little help with formating a number that needs to be written to a file. The vendor requires that numbers be formatted as follows -000000000000.00. if the number is positive then a zero should be displayed instead of + so if the above was positive it would be 000000000000.00.

My snippet of code is as follows:
if ($k == $sumIdx)
{
$summaryRec = $summaryRec .$prevRec[$k];
}
else
{
if ($k == 3)
{
$tmpFormatNum = sprintf("%.2f",$prevRec[$k]);
$tmpFormatStr = sprintf("%015d",$tmpFormatNum);
$summaryRec = $summaryRec .$tmpFormatStr;
}
else
{
$summaryRec = $summaryRec .$prevRec[$k]. ",";
}

With the above the second sprintf drops the decimal places. So if I have 82.33 I get 000000000000082 instead of 000000000082.33.

I tried several different format statement to no avail. I was thinking of writing several lines of code to read the value one byte at a time. But I would rather user the format function instead. Any help would be greatly appreciated.

Thanks
Jose'
 
Hi

As I understand, you need this :
Code:
[navy]$tmpFormatNum[/navy] [teal]=[/teal] [b]sprintf[/b][teal]([/teal][green][i]"[highlight]%015.2f[/highlight]"[/i][/green][teal],[/teal][navy]$prevRec[/navy][teal][[/teal][navy]$k[/navy][teal]]);[/teal]


Feherke.
 
Thank you my friend that worked like a charm I had used $.2f before instead of %015.2f like you had .

Long hard week I'm having brain farts; time for a beer.

Thanks again for the help.

Jose'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top