# Rust format hex, binary, octal, and scientific notation

> Format Rust numbers as hex, binary, octal, or scientific notation with std::fmt suffixes, uppercase output, prefixes, width, and zero padding.

HTML: https://rustprint.com/format/number-bases/

Rust formats hexadecimal with `{:x}` or `{:X}`, binary with `{:b}`, octal with `{:o}`, and scientific notation with `{:e}` or `{:E}`. These suffixes select formatting traits rather than converting the value manually.

**Common numeric formatting types**

```rust
let value = 42;
assert_eq!(format!("{value:b}"), "101010");
assert_eq!(format!("{value:o}"), "52");
assert_eq!(format!("{value:x}"), "2a");
assert_eq!(format!("{value:X}"), "2A");
assert_eq!(format!("{:e}", 42.0), "4.2e1");
```

## Format numbers as hex, binary, or octal

| Syntax | Trait      | Example purpose              |
| ------ | ---------- | ---------------------------- |
| `{:b}` | `Binary`   | base 2                       |
| `{:o}` | `Octal`    | base 8                       |
| `{:x}` | `LowerHex` | lower-case base 16           |
| `{:X}` | `UpperHex` | upper-case base 16           |
| `{:e}` | `LowerExp` | scientific notation with `e` |
| `{:E}` | `UpperExp` | scientific notation with `E` |

The suffix selects a trait, not a conversion function. Any type that implements the corresponding trait can use that syntax, including custom types.

## Uppercase and lowercase hexadecimal

Lower and upper hexadecimal are separate formatting traits. `{:x}` selects `LowerHex`; `{:X}` selects `UpperHex`. Scientific notation follows the same pattern with `e` and `E`.

That makes the requested representation explicit in the format string. There is no later case-conversion step hidden behind the syntax.

## Add 0x, 0b, or 0o prefixes

`{:x}` produces hexadecimal digits but no `0x` prefix. Add the alternate flag to request the prefixed form:

```text
{:#x}
```

The same pattern applies to binary and octal. See [alternate formatting with #](/format/alternate-format/index.md) for the exact prefixes and its separate meaning for `Debug`.

## Width and zero padding still apply

Type suffixes can be combined with other formatting parameters. For example, hexadecimal output can have a minimum width and sign-aware zero padding. The order is defined by the format grammar, so it is better to build the specification from its parts than memorize punctuation.

Use the [format specifier grammar](/reference/format-specifier-grammar/index.md) when combining several flags at once.

## Sources

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

## Related RustPrint guides

- [Rust formatting traits - Display, Debug, hex, binary, and more](/reference/formatting-traits/index.md): Map Rust format syntax such as {}, {:?}, {:x}, {:b}, and {:e} to the std::fmt traits Display, Debug, LowerHex, Binary, and LowerExp.
- [Rust format hex with 0x, binary with 0b, and pretty Debug](/format/alternate-format/index.md): Use Rust's # format flag to add 0x, 0b, and 0o prefixes, combine them with zero padding, and request pretty Debug output with {:#?}.
- [Rust leading zeros, signs, and zero padding](/format/signs-and-zero-padding/index.md): Add leading zeros and explicit signs to Rust numbers with format width and sign-aware zero padding, including prefixed hex, binary, and octal.
