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

Unable to print floating point in current locale #1291

Closed
dconnet opened this issue Aug 30, 2019 · 4 comments
Closed

Unable to print floating point in current locale #1291

dconnet opened this issue Aug 30, 2019 · 4 comments

Comments

@dconnet
Copy link

dconnet commented Aug 30, 2019

Since fmt has switched to locale-independent, it's not possible to print floating point in a specific locale with specific formatting.

	double p = 3.1416;
	fmt::print("%{:.2f}\n", p);
	fmt::print("%{:.2n}\n", p);

The first line works (locale-independent). The 2nd prints 3.1

Edit: I just noticed it appears to be an off-by-one issue.

	fmt::print("%{:.3n}\n", p);

will print 3.14

@vitaut
Copy link
Contributor

vitaut commented Aug 31, 2019

The reason the second line prints 3.1 is because precision in n (and g) specifies the maximum number of digits to print:

For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

Looks like floating-point 'n' is missing from the docs though - I need to fix that.

@dconnet
Copy link
Author

dconnet commented Aug 31, 2019

Is there any way with 'n' to print with a specified number of places always? For instance, I need to always display 2 decimals (no matter how large the number is) - even for a number like 2.1 (which should display as 2.10).

@vitaut
Copy link
Contributor

vitaut commented Aug 31, 2019

You can request to output trailing zeros with #:

auto s = fmt::format("{:#.3n}", 2.1); // s == "2.10"

@vitaut
Copy link
Contributor

vitaut commented Aug 31, 2019

The 'n' specifier is documented now: https://fmt.dev/dev/syntax.html

@vitaut vitaut closed this as completed Aug 31, 2019
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