in reply to Can sprintf suppress leading zero for float < 1?

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\./\./;

Replies are listed 'Best First'.
Re^2: Can sprintf suppress leading zero for float < 1?
by ambrus (Abbot) on Apr 13, 2012 at 21:11 UTC

    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.
        Ah! Good call, guys.
Re^2: Can sprintf suppress leading zero for float < 1?
by RotoValue (Initiate) on Apr 14, 2012 at 01:31 UTC
    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.

        It's safer to place the modifier before the final letter (e.g. %.3Zf) or to pick a different letter (e.g. %.3F). That way, it doesn't restrict the strings into which the pattern can be placed.

        I wonder what C# does.