|
|
| Think about Loose Coupling | |
| PerlMonks |
Can sprintf suppress leading zero for float < 1?by RotoValue (Initiate) |
| on Apr 13, 2012 at 13:46 UTC ( #964932=perlquestion: print w/ replies, xml ) | Need Help?? |
|
RotoValue has asked for the
wisdom of the Perl Monks concerning the following question:
I use sprintf to format values, with format strings stored in a database. Is there some way to have the format string *not* show a leading zero for for 0 < $x < 1 ? I've tried searches (both broad internet and at this site) and come up empty (at least for Perl; C# apparently lets you make such a distinction).
The above code gives $str a value of '0.123'. I'd like some other value of $fmt such that the code would give $str a value of '.123'. I could create some wrapper around sprintf to remove the leading 0 when it exists, but it would be much more elegant if I could find a format string that would produce the desired result without further code. And also more practical: I currently store format strings in a database, and at several points in my code I use sprintf to format values based on those database format strings; so if I need to generate my own wrapper, then I would also need to find, change, and test all those places.
Back to
Seekers of Perl Wisdom
|
|