Skip to main content

engineering-format

engineering-format is a tiny Python library for SI/metric prefix formatting and parsing.

It gives you two public helpers:

  • si_format(value) wraps a numeric value so it formats with an SI prefix through format() or f-strings.
  • si_parse(text, numeric_type=float) parses an SI-prefixed string back into a number.

The library is intentionally narrow in scope: it handles prefixes, not full unit systems. Append units outside the formatted expression when you need them.

Installation

Install from PyPI with uv:

uv add engineering-format

or:

uv pip install engineering-format

Install from a source checkout with uv:

uv sync

If you want to use it in another project from a checkout, install it in editable mode:

uv pip install -e .

Run All Examples

This repository includes a runnable examples script that mirrors the README code samples:

uv run python examples/readme_examples.py

Usage

Formatting

Wrap a value with si_format() and pass it to format() or an f-string:

from engineering_format import si_format

print(f"{si_format(4700):.2f}")
print(format(si_format(1_000_000), ".3f"))
print(f"{si_format(0.0047):.1f} Ω")

Output:

4.70k
1.000M
4.7m Ω

Formatting follows Python’s standard float-style format mini-language. Supported presentation types are the float-oriented ones: empty, e, E, f, F, g, G, n, and %.

Examples:

from engineering_format import si_format

print(f"|{si_format(1000):10.3f}|")
print(f"|{si_format(1000):<10.3f}|")
print(f"|{si_format(1000):^10.3f}|")
print(f"|{si_format(-1000):=+010.3f}|")

Output:

|    1.000k|
|1.000k    |
|  1.000k  |
|-00001.000k|

Parsing

Parse strings with si_parse():

from decimal import Decimal

from engineering_format import si_parse

print(si_parse("10k"))
print(si_parse("1_000k"))
print(si_parse("2.5m", Decimal))
print(si_parse("1 n"))
print(si_parse("1k/s"))

Output:

10000.0
1000000.0
0.0025
1e-09
1000.0

Parsing accepts:

  • SI prefixes from quecto q through quetta Q.
  • Micro in any of these forms: µ, μ, or u.
  • Leading and trailing whitespace.
  • An optional space between the number and prefix.
  • Underscores in numeric literals.
  • Trailing text after a valid <number><prefix> token.

When numeric_type is Decimal, parsing uses exact decimal arithmetic.

Supported Behavior

engineering-format is designed for compact engineering notation, not a general unit library.

What it does well:

  • Chooses an SI prefix automatically for formatted numbers.
  • Preserves normal Python formatting behavior for width, alignment, sign, grouping, and precision.
  • Parses common SI-prefixed strings back into numeric values.

What it does not do:

  • It does not attach or interpret units.
  • It does not validate that trailing text is a real unit.
  • It does not aim to parse arbitrary scientific notation formats beyond the supported numeric forms.

Examples

from decimal import Decimal

from engineering_format import si_format, si_parse

value = si_parse("4.7k")
print(value)
print(f"{si_format(value):.1f}Ω")
print(si_parse("2.5m", Decimal))

Output:

4700.0
4.7kΩ
0.0025

Contributing

Changes should come with tests when they affect formatting or parsing behavior.

CI runs on Python 3.12, 3.13, and 3.14 for every pull request and for pushes to main/master, and enforces both Ruff and pytest.

Run the checks before opening a pull request:

uv run pytest
uv run ruff check .

If you change documented behavior, update the examples in this README at the same time. The README is the main user-facing documentation for this project.

License

MIT License

Copyright (c) 2026 Karsten van Zwol

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

engineering_format-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

engineering_format-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file engineering_format-0.1.0.tar.gz.

File metadata

  • Download URL: engineering_format-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.5

File hashes

Hashes for engineering_format-0.1.0.tar.gz
Algorithm Hash digest
SHA256 05ff3472f12beb82fdace3828e336152712da862d4631ec32ddd6261f32285b4
MD5 368bb77fe0ef06e04df8ef2aa347ec14
BLAKE2b-256 93695a3df5da0bb96c45a3fb3ca293e71e4cf5df06989e8c1911e25fc8060a61

See more details on using hashes here.

File details

Details for the file engineering_format-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for engineering_format-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 016fa4762f4e21e8650d0985c76a097a9319a57a361121f2b0894f997b0be8e4
MD5 b51407aff1dd65f5cbf808701acd0fe4
BLAKE2b-256 15c69a3f1a16f045001ebf4f957594d938d38902d020359944404150f1e9fc7e

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page