Search code examples
rustprintingformattingwidth

How to print F32 with 1 dec place and a fixed width in rust


Trying to replicate the following C printf invocation

printf("%6.1f %6.1f %6.1f\n", x, y, z);

in rust and I can't find it in the docs. I need 6 positions wide with 1 decimal place.

First time I've been stumped finding what I need there.


Solution

  • Formatting strings are documented in the documentation for std::fmt.

    The equivalent of %6.1f would be {:6.1}:

    println!("{:6.1} {:6.1} {:6.1}", x, y, z);
    

    Playground