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

Data output - adding zero to single-digit numbers 1

Status
Not open for further replies.

yj7sum

Programmer
Jul 24, 2001
6
0
0
US
Please help. I am having problem with the following:

my data:

3:16 PM, 3:17 PM, 563.1234
3:17 PM, 3:16 PM, $ 563 . 1234
15: 03 , 2:02 p. ., $345.6
03: 3 p .m, 1:4 am, 1.999999999

my output when program is executed:

15:01 15:17 563.12 0: 1
15:01 15:16 563.12 23:59
15:00 2:02 345.60 10:59
15:03 1: 4 2.00 10: 1
: 0 : 0.00 24: 0

problem:

1. How do I add the zeros in front of the single digits numbers?
2. Why do I have an extra line at the end? What did I do wrong?
3. How do I change 1:4 to 1:04 and 10:1 to 10:01?

Your assistance is greatly appreciated.


 
1. How do I add the zeros in front of the single digits numbers?
A: This will format a number to a minimum of two digits, with leading zeroes, and store the result in the string:
Code:
$str = sprintf("%.2d", $num);

2. Why do I have an extra line at the end? What did I do wrong?
A: You're probably forgetting to chomp the linefeed off the end of your data records.

3. How do I change 1:4 to 1:04 and 10:1 to 10:01?

A: This code will format a time properly:
Code:
($hrs,$min) = split(/:/,$time);
$time = $hrs.':'.sprintf("%.2d",$min);
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Tracy,

Thanks so much for your quick reply.

yj7sum
PN
 
You're quite welcome. I hope it helps.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Be aware that as you work with 'numbers' and pad them with zeros, if you do any math with them (give perl any reason to treat them as numbers), your padding disappears. Get the value you want and pad just before output.




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top