Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Zero Padding to the Right of the Decimal

by davido (Cardinal)
on Jan 15, 2004 at 16:17 UTC ( [id://321597]=note: print w/replies, xml ) Need Help??


in reply to Zero Padding to the Right of the Decimal

You just have the order of things mixed up a little. Here's what you have:

my $i=0; while ($i <=100){ $i=sprintf("%3.2f",$i); $i=$i+0.01; print "$i\n"; }

So you turn $i into a string with padding, and then you increment by 0.01, which causes Perl to coerce the string into a number. At that point, string formatting is lost, and you get Perl's default numeric formatting. You could correct the problem by eliminating the sprintf and turning your print into printf, or simply increment $i before the sprintf statement instead of after, so that the effect sprintf has of coercing a value into a string isn't lost prior to the print.

Smile! Most languages don't even let you coerce strings into numeric values, and back again, without using two different variables and a conversion function. Perl does its best at Doing What You Mean.

Hope this helps.


Dave

Replies are listed 'Best First'.
Re: Re: Zero Padding to the Right of the Decimal
by Paulster2 (Priest) on Jan 15, 2004 at 21:48 UTC

    Although I knew the answer to this one (believe me they are far and few between, but I keep trying ;-}), the above node is OUTSTANDING. ++ to you davido. I wish more monks took the time to answer like this.

    Paulster2

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://321597]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found