Solved

printf to format columns

Posted on 2004-09-07
4
371 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now