Skip to main content

Intelligent value rounding and formatting for better numeric readability

Project description

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_value and, by extension, for format_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:

  1. Handling zero as a special case
  2. Using standard rounding for numbers ≥ 1
  3. For small numbers (< 1), finding the minimum number of decimal places needed to show a non-zero value
  4. 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

MIT License

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

smart_round-1.0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

smart_round-1.0.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file smart_round-1.0.1.tar.gz.

File metadata

  • Download URL: smart_round-1.0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for smart_round-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bf879f27bd2a2a8ff38161375f9a941b429d96368f7c59574949befeaa3a3676
MD5 344b30388097da582122247a57737de4
BLAKE2b-256 43bc6bad2f880aa96a469300eea97ab678c28df3532f3e58039ae5f9664d5a9e

See more details on using hashes here.

File details

Details for the file smart_round-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: smart_round-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for smart_round-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 769038134ee8a9a195709820786a98c24411622aa6ddeecbda2369b64e3e06ce
MD5 de999bde0f7e5fe6363a8f4d4fa0c9b0
BLAKE2b-256 829d3c2e8eaf06062f673fdff610bcb09c3e21e3ac67b14d452e2ff8c559eac8

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