Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision specifier for non-number arguments #90

Closed
lahwaacz opened this issue Jan 7, 2015 · 3 comments
Closed

Precision specifier for non-number arguments #90

lahwaacz opened this issue Jan 7, 2015 · 3 comments

Comments

@lahwaacz
Copy link

lahwaacz commented Jan 7, 2015

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.
@vitaut
Copy link
Contributor

vitaut commented Jan 7, 2015

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

@vitaut
Copy link
Contributor

vitaut commented Jan 8, 2015

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.

@vitaut vitaut closed this as completed Jan 8, 2015
@lahwaacz
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants