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).
my $fmt = '%.3f'; my $val = .123456; my $str = sprintf $fmt, $val;
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.
In reply to Can sprintf suppress leading zero for float < 1? by RotoValue
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |