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 format as binary number #1008

Closed
5 tasks done
Happypig375 opened this issue May 21, 2021 · 12 comments
Closed
5 tasks done

Printf format as binary number #1008

Happypig375 opened this issue May 21, 2021 · 12 comments

Comments

@Happypig375
Copy link
Contributor

Happypig375 commented May 21, 2021

Printf format as binary number

I propose we allow printf "%B" 10 which outputs 1010.

The existing way of approaching this problem in F# is going through Convert.ToString(..., 2) which is not needed for bases 8 and 16.

This is arguably used more than base 8.

Pros and Cons

The advantages of making this adjustment to F# are

  1. Convenience
  2. Consistency, as the standard bases (2, 8, 10, 16) should receive the same treatment.
  3. Conciseness

The disadvantages of making this adjustment to F# are that this brings multiple ways to do the same thing, but then %o and %x/%X exist.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

@dsyme
Copy link
Collaborator

dsyme commented May 21, 2021

Seems reasonable. You might want to check with OCaml since our printf design largely aligns with that

@Happypig375
Copy link
Contributor Author

https://stackoverflow.com/questions/39962412/why-is-b-deprecated-in-printf %b is deprecated and %B is recommended for booleans, and there was an attempt to redefine %b for binary numbers but got rejected.

@jackfoxy
Copy link

This would be a useful formatter, but %B having a different function than %b would be confusing. I can't think of another letter that would be intuitive. Maybe %t (for two)? Or if the parser could handle it %2?

@Happypig375
Copy link
Contributor Author

@jackfoxy %a and %A already do two different things. Same for %o and %O.

@Happypig375
Copy link
Contributor Author

Also %t is already defined.

@jackfoxy
Copy link

Darn it, I was looking at an obsolete reference. The problems with searching for documentation...

@Happypig375
Copy link
Contributor Author

@jackfoxy These have been in F# since 1.0. Are you looking at the right language?

@jackfoxy
Copy link

Nope, looked at the first (or at least most prominent) result of a Bing search for "F# printf".
I've long been of the opinion MS does a poor job of optimizing Bing searches into their own tech documentation.
In this case what I got was https://www.c-sharpcorner.com/article/printing-and-formatting-outputs-in-fsharp/ which to someone in a hurry (me) looked like a reliable source from its layout.

@Happypig375
Copy link
Contributor Author

Ah yes, c-sharpcorner.com, the website deemed as low quality by the C# subreddit.

@baronfel
Copy link
Contributor

For reference, a quick search for "F# printf" got me the official docs for printf specifiers in the language: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/plaintext-formatting#format-specifiers-for-printf

@Happypig375
Copy link
Contributor Author

@jackfoxy %2 won't work either because it's a width specifier like in %2d.

@Happypig375
Copy link
Contributor Author

Closing as implemented in dotnet/fsharp#11603

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

No branches or pull requests

5 participants