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.0.tar.gz (3.7 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.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for smart_round-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4b59b9b020f2b0fd2453417dd10cb625fe543c09e2411b5b69cf0e5090c3f87a
MD5 9049b973ec811be86ce4c6e4d8dd36b4
BLAKE2b-256 1934266e604e99adedfde7417b0d085589feabe8326463dfe1d4236c15f8ea61

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for smart_round-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ed0b30e810d81dfef0b779d00f2f4e4188f05ac394a5e73082156be80fc5074
MD5 25510f5a3618058af6021ba0744ec440
BLAKE2b-256 9a40bdde222b7cc1e42534c58076a4b1fa84481e0c72db815eb155e07a2e5ca6

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