Rust format specifier

Hi,

I am trying to format some floating point numbers with 0-padding for the integral part and a specified precision for the fractional part.

I have tried this:

println!("{:02.3}", 1.01);  

Although I expected 01.010, it displayed 1.010.

I had to resort to splitting the float into a integral and a fractional part and formatting them separately:

println!("{:02}.{:.03}", 1.1f64.floor(), (1.1f64.fract() * 1e3).floor());

Is this the intended behaviour of the formating ?
Is there a cleaner way of doing what I want ?
I am aware I can supply the precision as an argument that I could also use to shift the decimal point to the right of the fractional part, I am thinking fill/indent or other ?...

thanks

B

Looks like the first number is the total length, including the decimal.

println!("{:06.3}", 1.01); // 01.010

This first number refers to “minimal width” as per documentation, or minimal total length if you want to spell it this way. It has basically the same meaning as the first number in similar position in C’s printf. Unfortunately, by now this meaning is basically an industry standard no matter how inconvenient it is when formatting floats, you will see the same thing in Python’s format strings.

thank you !

RTFM (Read The Fabulous Manual) as always :wink:
Thanks for the clarification.