Skip to main content

Dynamic rounding for readable data - rounds based on order of magnitude

Project description

Dynamic Rounding (Python)

Version 0.1.2

Dynamic rounding for readable data — rounds based on order of magnitude.

Installation

pip install dynamic-rounding

With pandas support:

pip install dynamic-rounding[pandas]

Quick Start

Basic usage (no dependencies)

from dynamic_rounding import round_dynamic

# Single value — rounds to nearest half order of magnitude
round_dynamic(87054321)          # → 85000000
round_dynamic(4321)              # → 4500

# Custom offset
round_dynamic(87054321, offset=0)     # → 90000000 (nearest 10M)
round_dynamic(87054321, offset=-1)    # → 87000000 (nearest 1M)
round_dynamic(87054321, offset=-1.5)  # → 87000000 (nearest 500K)

# Dataset — larger values get finer precision
round_dynamic([4428910, 983321, 42109])
# → [4500000, 1000000, 40000]

With pandas

from dynamic_rounding.pandas import round_dynamic_series
import pandas as pd

# Rounds Series with set-aware precision
round_dynamic_series(df['revenue'])

# Dataset mode with custom offsets
round_dynamic_series(df['revenue'], offset_top=-0.5, offset_other=0)

# Parses formatted strings automatically
s = pd.Series(["$1,200", "(500)", "4,428,910.41"])
round_dynamic_series(s)  # → [1000.0, -500.0, 4500000.0]

Features

  • Type preservation: Returns int if input was int and result is whole number
  • None handling: None input returns None (not 0)
  • Pass-through: Non-numeric values pass through unchanged by default
  • String parsing (pandas): Parses $, , £, ¥, commas, and (500)-500

API

round_dynamic(data, offset=None, offset_top=None, offset_other=None, num_top=1, enforce_numeric=False)

Single mode (when data is a number):

  • data: Number to round
  • offset: OoM adjustment (default: -0.5)
  • enforce_numeric: If True, raises ValueError for non-numeric input

Dataset mode (when data is a list):

  • data: List of numbers to round
  • offset_top: OoM adjustment for top magnitude(s) (default: -0.5)
  • offset_other: OoM adjustment for other magnitudes (default: 0)
  • num_top: How many top orders get offset_top (default: 1)
  • enforce_numeric: If True, raises ValueError for non-numeric values in list

round_dynamic_series(series, offset=None, offset_top=None, offset_other=None, num_top=1, enforce_numeric=False)

Same parameters as round_dynamic, but operates on a pandas Series.

  • Parses formatted strings automatically (currency, commas, accounting negatives)
  • Returns a new Series (original unchanged)

Offset values

Offset Meaning 87,054,321 rounds to
1 one OoM coarser 100,000,000
0 current OoM 90,000,000
-0.5 half of current OoM (default) 85,000,000
-1 one OoM finer 87,000,000
-1.5 half of one OoM finer 87,000,000

Development

cd python
pip install -e ".[dev]"
python3 -m pytest tests/ -v

License

MIT

See Also

Project details


Download files

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

Source Distribution

dynamic_rounding-0.1.2.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

dynamic_rounding-0.1.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file dynamic_rounding-0.1.2.tar.gz.

File metadata

  • Download URL: dynamic_rounding-0.1.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for dynamic_rounding-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cfbd4fb925d66166ca3901149b8018ce74943e68a33e065760c9f6cc0668074a
MD5 3a7f1e10557b54a29593d6740795cb39
BLAKE2b-256 936f1148e6037091f3de42e74976791e37d41288086549e8eea6c5eb0a11bd96

See more details on using hashes here.

File details

Details for the file dynamic_rounding-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for dynamic_rounding-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fb9f4684f3bbcd51a60f751610f88387c549bf2eab58893cd52508c554fe0cbb
MD5 e13a5c4d9cc7190034407c0b2ee17980
BLAKE2b-256 a5363700101c10105d68949b36f30eb37700cd74ce9e128c7ca22b47c8add330

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page