Skip to content

Rust format float decimals and precision

For a Rust floating-point value, "{:.2}" means two digits after the decimal point. It formats 3.14159 as 3.14 and keeps trailing zeros when they are part of the requested precision, so 5.0 becomes 5.00.

EXAMPLE: CI-CHECKEDTEST ASSERTED · examples/format-output/src/format_pages.rs#page-precision-rules
assert_eq!(format!("{:.2}", 3.14159), "3.14");
assert_eq!(format!("{:.2}", 5.0), "5.00");
assert_eq!(format!("{:>8.2}", 3.5), "    3.50");
assert_eq!(format!("{:.4}", "rustacean"), "rust");
assert_eq!(format!("{:.2e}", 1234.567), "1.23e3");

let precision = 3;
let value = 1.23456;
assert_eq!(format!("{value:.precision$}"), "1.235");

For floating-point formatting, precision controls the number of digits after the decimal point. "{:.2}" formats 3.14159 as 3.14.

This is formatting, not a change to the stored floating-point value. Use it when you need a particular textual representation for output.

A fixed precision also keeps the requested number of fractional digits in the formatted output. With "{:.2}", a finite value such as 5.0 is rendered as 5.00.

That makes precision the right control when output needs a stable number of visible decimal places. Do not use width for this job: width pads the whole formatted value but does not choose how many fractional digits are shown.

Width and precision can appear in the same replacement field. In "{:>8.2}", .2 first controls the floating-point representation and >8 then right-aligns that representation in a minimum width of eight columns.

For example, 3.5 becomes 3.50 and is then padded on the left to fill the requested width. See width, alignment, and fill for the padding rules.

The same precision syntax works with exponent notation. "{:.2e}" keeps two digits after the decimal point in the mantissa.

Use number bases and exponent notation when the output type itself is the main question.

For non-numeric formatting, precision behaves like a maximum width. If the formatted result is longer, Rust truncates it before applying any requested width and alignment.

That makes precision useful when a text field must not exceed a certain displayed length. It is different from width: width pads short output, while precision can limit long output.

Precision is ignored for integral formatting. If you need a minimum number of visible positions for an integer, use width and zero padding instead:

{:05}

That is a width of five with sign-aware zero padding, not a precision of five.

Precision can come from a usize formatting argument instead of being written directly into the format string. A captured value can be used with syntax such as "{value:.precision$}".

Rust also supports positional and explicit named parameter forms, plus the special asterisk form documented by std::fmt. Keep the mechanism separate from the meaning of precision itself: the parameter chooses the number, while the formatted type decides what that number means.

See arguments and captured variables for the broader argument-selection model.