Scientific Notation for Float

Hi, If I print floats sometimes they be written in scientific notation sometimes not. I don't know what causes this but I always want to see it with scientific notation. How can I achieve this ?

You can encounter what I mean is in this screenshot multiple times

I believe you are looking for the e formatter:

fn main() {
    let x = 42.0;
    assert_eq!(format!("{x:e}"), "4.2e1");
}

Playground.

1 Like

Thanks this helped but why sometimes floating points shows themselves like e notation automatically(like in the screenshot coordinates section, I had a data structure and I did print with {:#?} formatter ) ?

The reason for that is the Debug implementation for f32 and f64, which changes the format based on the value at hand.

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.