Skip to content

Pretty print isn't pretty for floats #29472

Closed
@therustmonk

Description

@therustmonk

The following code:

fn main() {
    println!("{}", 1.2999999999999999);
    println!("{:?}", 1.2999999999999999);
    println!("{:.3}", 1.2999999999999999);
    println!("{:.*}", 5, 1.2999999999999999);

    // aren't pretty too
    println!("{:#}", 1.2999999999999999);
    println!("{:#?}", 1.2999999999999999);
}

prints:

1.2999999999999998
1.2999999999999998
1.300
1.30000
1.2999999999999998
1.2999999999999998

C behaviour:

#include <stdio.h>
void main() {
  double f = 1.2999999999999999999;
  printf("%.14g", f);
}

prints:

1.3

With Rust everything works like f, but not like g.

Is that really no way to get behaviour like g option of printf?
It's highly important feature for serialization libs.

rustc 1.6.0-nightly (8ca0acc 2015-10-28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API 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