Skip to content

scientific notation incorrect decimal places #115737

Closed
@ljdoig

Description

@ljdoig

I tried this code:

    assert_eq!("1.0e3", format!("{:.1e}", 1001));
    assert_eq!("1.0e3", format!("{:.1e}", 1000));
    assert_eq!("1.0e3", format!("{:.1e}", 999));

I expected this all to be fine.

Instead, the final assertion fails, and the formatting is done like this in the final case:

    assert_eq!("1.00e3", format!("{:.1e}", 999));

Not sure why 2 decimal places are returned, looks like a bug.
This bug also exists in the beta or nightly versions, running in Rust Playground.

Discussion: https://users.rust-lang.org/t/scientific-notation-decimal-places/99682

Edit: after looking around the compiler code for the first time I think the problem is somewhere in either format_exact or format_exact_opt.

Metadata

Metadata

Assignees

Labels

A-fmtArea: `core::fmt`C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions