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

Add examples to fmt precision #24662

Merged
merged 2 commits into from
Apr 25, 2015
Merged

Add examples to fmt precision #24662

merged 2 commits into from
Apr 25, 2015

Conversation

steveklabnik
Copy link
Member

Fixes #24656

r? @pnkfelix

I just added the examples, but if the wording needs expanded too, let me know what you think should be added :)

//! println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
//! println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
//! println!("Hello {} is {:.*}", "x", 5, 0.01);
//! println!("Hello {} is {2:.*}", "x", 5, 0.01);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps have a comment above each one reading out what each one means? Something like "Hello {first argument (x)} is {second argument (0.01) with precision specified in zeroeth argument (5)}"

@Manishearth
Copy link
Member

r=me with or without comments addressed, unless you specifically wanted Felix's r 😄

@pnkfelix
Copy link
Member

@steveklabnik the additions seem fine (i.e. r=me); the suggestions noted by @Manishearth also seem like good changes.

Its funny (or perhaps sad); my original issue #24656 was that I could not find documentation for the actual meaning of .*.

@pnkfelix
Copy link
Member

Although, reading over that text from https://github.com/rust-lang/rust/blob/master/src/libcollections/fmt.rs#L106 now, it seems like there is a little room for improvement.

Namely: While that text does describe the meaning of {:.*}, it does not describe the meaning of {:.<num>}.

I.e. I might have guessed that {:.5} means that it should print five decimal places, but it actually means lookup the number of decimal places to print from the argument number five (right? That's what I at least infer from my reverse engineering in those examples I wrote.)

Update: I goofed, so I have crossed out some erroneous text from the above. Namely, the examples used {:.5$}, not {:.5}; the $ is what makes it interpret the 5 as "argument number five."

@pnkfelix
Copy link
Member

So yes, on further reflection, while these changes are good, I think one more thing is needed: An expansion of the text at http://doc.rust-lang.org/nightly/std/fmt/index.html#precision that explains how the actual precision argument as written is interpreted.

And then you should probably consider moving my examples that you have transcribed into that section on the precision formatting parameter. Update: Ugh, I goofed again, you did put them into that section.

@Manishearth
Copy link
Member

I think an explicit mention of the differences between * and $, and some more examples, would be helpful.

@pnkfelix
Copy link
Member

So here is a draft of what we might write in the new precision section:

Precision

...
For floating-point types, this indicates how many digits after the decimal point should be printed.

There are three possible ways to specify the desired precision: (1.) an integer .N, (2.) an integer followed by dollar sign .N$, or (3.) an asterisk .*.

  • The first specification, .N, means the integer N itself is the precision.
  • The second, .N$, means use format argument N (which must be a usize) as the precision.
  • Finally, .* means that this {...} is associated with two format inputs rather than one: the first input holds the usize precision, and the second holds the value to print. Note that in this case, if one uses the format string {<arg>:<spec>.*}, then the <arg> part refers to the value to print, and the precision must come in the input preceding <arg>.

For example, these:

// Hello {arg 0 (x)} is {arg 1 (0.01} with precision specified inline (5)}
println!("Hello {0} is {1:.5}", "x", 0.01);

// Hello {arg 1 (x)} is {arg 2 (0.01} with precision specified in arg 0 (5)}
println!("Hello {1} is {2:.0$}", 5, "x", 0.01);

// Hello {arg 0 (x)} is {arg 2 (0.01} with precision specified in arg 1 (5)}
println!("Hello {0} is {2:.1$}", "x", 5, 0.01);

// Hello {next arg (x)} is {second of next two args (0.01} with precision
//                          specified in first of next two args (5)}
println!("Hello {} is {:.*}",    "x", 5, 0.01);

// Hello {next arg (x)} is {arg 2 (0.01} with precision
//                          specified in its predecessor (5)}
println!("Hello {} is {2:.*}",   "x", 5, 0.01);

All print the same thing:

Hello x is 0.01000

While these:

println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");

print two significantly different things:

Hello, `1234.560` has 3 fractional digits
Hello, `123` has 3 characters

Update: added a fifth example to the first series of Hello's to illustrate the .N format.

@pnkfelix
Copy link
Member

(ah good doc is so hard...)

@Manishearth
Copy link
Member

👍

@steveklabnik
Copy link
Member Author

(ah good doc is so hard...)

Right? Thanks so much :)

@steveklabnik
Copy link
Member Author

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Apr 24, 2015

📌 Commit eb5b842 has been approved by steveklabnik

@steveklabnik
Copy link
Member Author

(Since @pnkfelix ended up writing the doc, i'll r+ it)

@steveklabnik
Copy link
Member Author

Sneaking this into #24786 as well

@steveklabnik
Copy link
Member Author

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Apr 24, 2015

📌 Commit 064972c has been approved by steveklabnik

steveklabnik added a commit to steveklabnik/rust that referenced this pull request Apr 25, 2015
Fixes rust-lang#24656 

r? @pnkfelix 

I just added the examples, but if the wording needs expanded too, let me know what you think should be added :)
bors added a commit that referenced this pull request Apr 25, 2015
@bors bors merged commit 064972c into rust-lang:master Apr 25, 2015
@steveklabnik steveklabnik mentioned this pull request Apr 25, 2015
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

Successfully merging this pull request may close these issues.

std::fmt needs doc for precision := '*' case
4 participants