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

extend fmt! to add %g analogous to printf '%g' #3810

Closed
erickt opened this issue Oct 18, 2012 · 9 comments
Closed

extend fmt! to add %g analogous to printf '%g' #3810

erickt opened this issue Oct 18, 2012 · 9 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@erickt
Copy link
Contributor

erickt commented Oct 18, 2012

It's sometimes nice not to include trailing zeroes when printing floats. It'd be nice if we copied printf's %g to handle this.

@pcwalton
Copy link
Contributor

I don't think this is a backwards compatibility issue. Nominating for recategorization.

@graydon
Copy link
Contributor

graydon commented Jul 11, 2013

just a bug, removing milestone/nomination.

@pnkfelix
Copy link
Member

Visiting for triage, email from 2013 sep 23

We're deprecated fmt!; now there's format!. Updated bug title to reflect this.

Its not 100% clear from the format! documentation, but it seems to me like {:f} does not print trailing zeroes by default.

@pnkfelix
Copy link
Member

I wrote some test code. The {:f} format specification not only omits trailing zeroes by default, it also omits the decimal point if only zeroes follow the decimal point. (I have put test code at the end of this comment.)

If this were a dynamically typed language (like Lisp or JavaScript), I'd file a bug saying that its important to distinguish exact integers from inexact (floating-point) values that look like integers. But we're statically typed, so that argument does not hold up.

(I do wonder whether some people might want a way to specify that the decimal point and at least one trailing decimal is always output. But it seems like that should be filed as a separate bug at this point.)

Felix's test code follows

fn main() {

    macro_rules! demo(
        ($fmt:expr, $num:expr) =>
        ( println!("demo format!({:>9s}, {:>21s}) \t=> {:s}",
                   format!("\"{:s}\"", $fmt), stringify!($num), format!($fmt, $num)) )
    )

    demo!(     "{}", 1.00);
    demo!(   "{:f}", 1.00);
    demo!(  "{:0f}", 1.00);
    demo!("{:0.0f}", 1.00);
    demo!("{:0.1f}", 1.00);
    demo!("{:0.2f}", 1.00);
    demo!(   "{:f}", 1.20);
    demo!(  "{:0f}", 1.20);
    demo!( "{:02f}", 1.20);
    demo!( "{:20f}", 1.20);
    demo!( "{:08f}", 1.20);
    demo!( "{:.3f}", 1.20);
    demo!( "{:0.f}", 1.20);
    demo!(  "{:+f}", 1.20);
    demo!( "{:.1f}", 1.20);
    demo!( "{:.2f}", 1.20);
    demo!( "{:.3f}", 1.20);
    demo!( "{:.4f}", 1.20);

    demo!( "{:.1f}", 1.24);
    demo!( "{:.2f}", 1.24);
    demo!( "{:.3f}", 1.24);
    demo!( "{:.4f}", 1.24);

    demo!( "{:.1f}", 1.25);
    demo!( "{:.2f}", 1.25);
    demo!( "{:.3f}", 1.25);
    demo!( "{:.4f}", 1.25);

    demo!( "{:.1f}", 1.26);
    demo!( "{:.2f}", 1.26);
    demo!( "{:.3f}", 1.26);
    demo!( "{:.4f}", 1.26);

    demo!(   "{:f}", 1.123456789123456789f);
    demo!( "{:.0f}", 1.123456789123456789f);
    demo!("{:0.0f}", 1.123456789123456789f);
    demo!("{:0.1f}", 1.123456789123456789f);
    demo!( "{:.1f}", 1.123456789123456789f);
    demo!("{:6.1f}", 1.123456789123456789f);
    demo!("{:6.3f}", 1.123456789123456789f);
    demo!( "{:.8f}", 1.123456789123456789f);
    demo!("{:.20f}", 1.123456789123456789f);
}

@pnkfelix
Copy link
Member

(closing as "fixed" since {:f} already omits trailing zeros. Please open a fresh bug if you have a use-case for an alternative behavior.)

@pnkfelix pnkfelix reopened this Sep 30, 2013
@pnkfelix
Copy link
Member

I was misreading the man page for printf. @huonw pointed out to me that I overlooked an important aspect of its behavior: It dynamically selects between %f versus %e, based on which variant produces less output.

This is a demonstration from python:

>>> '%g' % 1e-10
'1e-10'
>>> '%f' % 1e-10
'0.000000'
>>> '%g' % 1e-2
'0.01'
>>> '%f' % 1e-2
'0.010000'

I'm not sure we want to provide this option, but I'll leave the ticket open in case others want to comment.

(Also, I do not think format! has support yet for something analogous to %e.)

@sanxiyn
Copy link
Member

sanxiyn commented Feb 10, 2014

Triage. %e support(#6593) landed recently.

@reem
Copy link
Contributor

reem commented Dec 15, 2014

Triage: nothing has changed, still not sure we actually want this feature.

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#844

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
fix erroneous flattening of `{self}` in imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

7 participants