# Rust format float decimals and precision

> Format Rust floats to 2 decimal places, control precision for scientific notation and strings, and understand why integer precision is ignored.

HTML: https://rustprint.com/format/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`.

**Float decimals, trailing zeros, width, strings, exponent notation, and dynamic precision**

```rust
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");
```

## Format a float to 2 decimal places

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.

## Keep trailing zeros

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.

## Combine width and precision

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](/format/width-alignment-fill/index.md) for the padding rules.

## Scientific notation

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](/format/number-bases/index.md) when the output type itself is the main question.

## Strings and other non-numeric output

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.

## Integral values

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

```text
{:05}
```

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

## Dynamic precision

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](/format/arguments-and-capture/index.md) for the broader argument-selection model.

## Sources

- [std::fmt precision](https://doc.rust-lang.org/std/fmt/#precision)

## Related RustPrint guides

- [Rust format strings - syntax, width, precision, and traits](/format/format-strings/index.md): Read Rust format strings from {} through argument capture, width, precision, alignment, escaping, and formatting traits.
- [Rust format arguments: positional, named, and captured variables](/format/arguments-and-capture/index.md): Use implicit, positional, named, and captured arguments in Rust format strings, including println! with captured variables and how argument selection ends at the colon.
- [Rust pad and align strings with width and fill](/format/width-alignment-fill/index.md): Pad Rust strings and formatted values with minimum width, left, center, or right alignment, and custom fill characters.
