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

@printf does not accept * for passing precision or width as argument #40654

Closed
Maxpxt opened this issue Apr 29, 2021 · 3 comments · Fixed by #40105
Closed

@printf does not accept * for passing precision or width as argument #40654

Maxpxt opened this issue Apr 29, 2021 · 3 comments · Fixed by #40105
Labels
domain:strings "Strings!" kind:feature Indicates new feature / enhancement requests

Comments

@Maxpxt
Copy link

Maxpxt commented Apr 29, 2021

The docs on @printf state that it follows C-style formatting as specified here. This spec allows for * to be used in the precision and/or width positions to indicate that the precision and/or width will be passed as additional arguments preceding the value to be formatted. Example (in C):

printf("%.*f", 2, 3.141);   // prints "3.14"
printf("%.*f", 4, 3.141);   // prints "3.1410"

Currently (Julia 1.6.1), @pritnf does not recognize such format specifiers. @printf "%.*f" 2 3.141 fails with ArgumentError: invalid format string: '%.*f', invalid type specifier: '*'.

I don't know whether not supporting this is intended behavior or not. If it is, the docs should be updated to say how Julia's @printf differs from the C-style spec.

Here is an example where having this would be useful:

function format_float_without_trailing_zeros(x, precision)
    rstrip(rstrip((@sprintf "%.*f" precision x), '0'), '.')
end

format_float_without_trailing_zeros(3.1415, 2)  # "3.14"
format_float_without_trailing_zeros(3.0, 2)     # "3"
@bicycle1885
Copy link
Member

#40105 addresses this issue (Fmt.jl supports that feature, btw).

@Maxpxt
Copy link
Author

Maxpxt commented Apr 30, 2021

Oh, I did not think to search the PRs. Should the issue be left open? (Thanks for the tip!)

@bicycle1885
Copy link
Member

Should the issue be left open?

Yes, I think it should.

@musm musm linked a pull request May 2, 2021 that will close this issue
@StefanKarpinski StefanKarpinski added kind:feature Indicates new feature / enhancement requests domain:strings "Strings!" labels Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:strings "Strings!" kind:feature Indicates new feature / enhancement requests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants