smart_round
A Python package that provides intelligent value rounding and formatting for better numeric readability.
Features
- smart_round: Intelligently round float values, preserving significant digits for small numbers
- format_value: Convert float values to nicely formatted strings with appropriate precision
- format_dataframe: Format all float columns in a pandas DataFrame for better display
Installation
pip install smart_round
Dependencies
- NumPy (required for
format_valueand, by extension, forformat_dataframe) - pandas (required for
format_dataframe)
These dependencies are optional if you only need the basic smart_round function.
Usage Examples
Basic rounding with smart_round
from smart_round import smart_round
# Regular numbers round to the specified decimal places
smart_round(1.2345, tail=2) # Returns 1.23
# Very small numbers keep enough decimal places to show at least one significant digit
smart_round(0.00123, tail=2) # Returns 0.0012 instead of 0.00
# Zero is handled as a special case
smart_round(0, tail=2) # Returns 0.0
Formatting values with format_value
from smart_round import format_value
import numpy as np
# Format regular numbers
format_value(1.2345) # Returns '1.235'
# Format very small numbers while preserving significant digits
format_value(0.00123) # Returns '0.001'
# Handle NaN values gracefully
format_value(np.nan) # Returns ''
# Ensure decimal point is followed by at least one digit
format_value(1.0) # Returns '1.0' not '1.'
Working with pandas DataFrames
from smart_round import format_dataframe
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({
'Value': [1.23456, 0.00123, 123.45],
'Other': ['A', 'B', 'C']
})
# Format float columns
formatted_df = format_dataframe(df)
print(formatted_df)
Output:
Value Other
0 1.235 A
1 0.001 B
2 123.45 C
How it works
The smart_round function works by:
- Handling zero as a special case
- Using standard rounding for numbers ≥ 1
- For small numbers (< 1), finding the minimum number of decimal places needed to show a non-zero value
- Rounding to that number of places or the requested places, whichever is larger
This makes the function particularly useful for displaying data with varying magnitudes while maintaining readability.
License
Release files for smart-round 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| smart_round-1.0.1.tar.gz | 3.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| smart_round-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.7 kB
Release files / smart_round-1.0.1.tar.gz
| Download URL | smart_round-1.0.1.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bf879f27bd2a2a8ff38161375f9a941b429d96368f7c59574949befeaa3a3676
|
|
BLAKE2b-256 checksum How to use checksums |
43bc6bad2f880aa96a469300eea97ab678c28df3532f3e58039ae5f9664d5a9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / smart_round-1.0.1-py3-none-any.whl
| Download URL | smart_round-1.0.1-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
769038134ee8a9a195709820786a98c24411622aa6ddeecbda2369b64e3e06ce
|
|
BLAKE2b-256 checksum How to use checksums |
829d3c2e8eaf06062f673fdff610bcb09c3e21e3ac67b14d452e2ff8c559eac8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|