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).

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.

Replies are listed 'Best First'.
Re: Can sprintf suppress leading zero for float < 1?
by temporal (Pilgrim) on Apr 13, 2012 at 14:49 UTC

    I don't think this is possible within sprintf. All floating point numbers have to have a digit before the decimal point.

    The standard for printf family of functions states:

    f, F
    The double argument is rounded and converted to decimal notation in the style -ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.

    Not too difficult to do with a quick regex or something, though.

    $str =~ s/^0\./\./;

      Needs a -?

        Or a \b instead of a ^. That not only covers leading -, it also covers leading + ("%+f"), leading space ("% f") and cases where something else preceeds the %f.
      If a decimal point appears, at least one digit appears before it.

      Thanks for highlighting that! I must have glazed over that sentence in the man pages.

      So unfortunately sprintf can't do what I want. I'd like it to support a different conversion specifier that behaved like f, except would omit the leading zero.

      Not too difficult to do with a quick regex or something, though.

      I agree - my issue isn't so much stripping off the leading zero, but finding the places in my code where I'm currently calling sprintf and replace them with a new wrapper which does it.

      Thanks to all for the suggestions.

        RotoValue:

        Perhaps you can replace the sprintf function with a wrapped version. I've never replaced the core functions before, but I've seen references to it. Googling for perl replace core function came up with a couple useful looking links:

        So perhaps you could do something like:

        BEGIN{ *CORE::GLOBAL::old_sprintf = *CORE::GLOBAL::sprintf; *CORE::GLOBAL::sprintf = sub { my $formatted_string = old_sprintf(@_); # 0.###Z ==> .### $formatted_string =~ s/([^0-9])0(\.\d+)Z/$1$2/g; return old_sprintf(@_); } };

        Which I adapted (probably incorrectly) from Corion's stack overflow post (the second link). I've never tried to override the core functions, but something like this might be a good solution for you. Of course, you may need to write some magic code to recognize the new format string, and figure out how to do the replacement(s) without messing up other replacements.

        If you could do something cheesy like use a format like "blah blah %.fZ blah", and could rely on Z never being at the end of a number in your code, then you could do a replacement like the one I did above.

        Update: Added "something like".

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Can sprintf suppress leading zero for float < 1?
by ww (Archbishop) on Apr 13, 2012 at 14:54 UTC
    There's probably something I'm missing about your circumstances...
    and this is, admittedly, not terribly elegant...
    but -- as printf (referred from sprintf says (loosely paraphrased), don't use (s)printf when print would do.
    So -- on the off-chance -- would this be acceptable?
    my $str; my $val = 0.123456; ($str = $val ) =~ s/^0//; say "\$str: $str";

    Output: $str: .123456

    Updateprintf caution added

      That doesn't output .123.

        No, it doesn't.

        As posted, it outputs:

        $str: .123456

        when executed on Win7 with 5.014:

        This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x +86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2011, Larry Wall Binary build 1402 [295342] provided by ActiveState http://www.ActiveSt +ate.com Built Oct 7 2011 15:49:44

        Update: Head slap moment!

        Are you getting a different output? If so, with which perl version?

        No it doesn't. Truncating the original value is left as an exercise (already solved) by OP. So...

        #!/usr/bin/perl use 5.014; # 964932 my $str; my $val = 0.123456; my $fmt = '%.3f'; my $val_trunc = sprintf $fmt, $val; ($str = $val_trunc ) =~ s/^0//; say "\$str: $str";

        Output: $str: .123

Re: Can sprintf suppress leading zero for float < 1?
by JavaFan (Canon) on Apr 13, 2012 at 14:42 UTC
    Is there some way to have the format string *not* show a leading zero for for 0 < $x < 1 ?
    No.