Gossamer Forum
Home : General : Perl Programming :

need help with sprintf

Quote Reply
need help with sprintf
I'm having trouble getting sprintf to cooperate with me. I want to take a number such as "4.0234", pad up to one 0, and round to 2 decimals, such that I would arrive at "04.02". Seems I can use "%0.2d" to pad zeroes and "%.2f" to round decimals, but I can't figure out how to combine the two. I'd appreciate some assistance on this. Thanks!
Quote Reply
Re: [lostinva] need help with sprintf In reply to
How about something like:
Code:
my $num = 4.0234;
$num = sprintf("%05s", sprintf("%.2f", $num));

Where the inner sprintf pads limits the precision, and the outer one uses it as a string to pad it.

Adrian

Last edited by:

brewt: Apr 15, 2009, 5:43 PM
Quote Reply
Re: [brewt] need help with sprintf In reply to
Perfect! And that explains why what I had tried wasn't working.

Is there any way this can be accomplished in a SQL query since this is where the data is originating from? There doesn't seem to be an equivalent function built-in unless I've just overlooked it.