Solved

printf to format columns

Posted on 2004-09-07
4
375 Views
Last Modified: 2008-02-01
Hello All!

I am new to Perl, so please forgive my limited knowledge...

I am reading in a CSV file and outputting the contents to several proprietary data formats that we use in-house.  In order to do this, I need to reformat some of my data to fit within a 5 character column width.

I need to format as in the following example (note - column width is 5 characters):
1 -> 1.000
1.25 -> 1.250
23.2 -> 23.20

According to my reading, I should probably use something like printf("%0-5s","1.25") in order to format 1.25 to 1.250 however this isn't working for me!?!?

Any ideas???

Thanks!
0
Comment
Question by:AirResourceSpec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 4

Expert Comment

by:divt
ID: 11999714
Here a rough sample.

CODE:

$n[0]=0.1;
$n[1]=0.12;
$n[2]=2.3;
$n[3]=45.67;
$n[4]=980;
$n[5]=123.456;
$n[6]=1234.567;
$n[7]=12345.678;

foreach $current_n (@n) {
    $formatted_n = substr(sprintf("%5.3f", $current_n), 0, 5);
    print "$formatted_n \n";
}

OUTPUT:

0.100
0.120
2.300
45.67
980.0
123.4
1234.
12345


Notice the problem when you have 4-digit integer.  Would you accept "1234." or rather "1234" ?
0
 
LVL 4

Accepted Solution

by:
divt earned 250 total points
ID: 11999745
Just thought I should include your sample numbers:

CODE:

$n[0]=1;
$n[1]=1.25;
$n[2]=23.2;
$n[3]=0.1;
$n[4]=980;
$n[5]=1234;  
$n[6]=12345;  
$n[7]=12345.678;

foreach $current_n (@n) {
    $formatted_n = substr(sprintf("%5.3f", $current_n), 0, 5);
    print "$formatted_n \n";
}


OUTPUT:

1.000
1.250
23.20
0.100
980.0
1234.
12345
12345
0
 
LVL 1

Author Comment

by:AirResourceSpec
ID: 12000253
So far, so good... I only have time to do a bit of testing today, but so far this seems to be working great!  Thanks!!!
0
 
LVL 4

Expert Comment

by:divt
ID: 12004694
Glad to hear so.  :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question