Skip to content

Precision specifier for non-number arguments #90

Closed

Description

@lahwaacz

From the docs:

The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating-point value formatted with 'f' and 'F', or before and after the decimal point for a floating-point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values.

So I tried to use precision on a string argument, for example:

std::cout << fmt::format("{0:.2s}", "test") << std::endl;

which results in segfault:

terminate called after throwing an instance of 'fmt::FormatError'
  what():  precision specifier requires floating-point argument
Aborted (core dumped)

Also note that printf allows the precision specifier even for integer arguments:

Precision specifies the minimum number of digits to appear.

Activity

  1. vitaut commented on Jan 7, 2015

    @vitaut
    Contributor

    Good catch, I'll have a look into this. Thanks!

  2. vitaut commented on Jan 8, 2015

    @vitaut
    Contributor

    Fixed in 43aebf5. As for "precision" for integer arguments, the fmt::format function follows Python's conventions in not allowing it, but as in Python you can use a combination of fill and width for similar effect:

    fmt::format("{:05}", 42); // returns "00042"
    

    or use fmt::sprintf which follows printf conventions and allows precision for integer arguments.

  3. lahwaacz commented on Jan 11, 2015

    @lahwaacz
    Author

    Thanks, I was not aware of Python's approach so in that case everything is all right...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions