Cost Rounding Utility
Relevant source files
The following files were used as context for generating this wiki page:The Cost Rounding Utility is a key feature for maintaining data integrity of monetary values within the POS system. It comprises two distinct rounding mechanisms: a standalone script for bulk-updating prices to whole integers, and an integrated function within the main application that ensures all costs and prices are consistently rounded to one decimal place. This system prevents floating-point inaccuracies and standardizes financial data stored in the pos_inventory.db database.
Sources: round_costs.py, pos_app.py:11-17, 126-140
Core Rounding Implementations
The project utilizes two different functions for rounding, each serving a specific purpose.
Sources: round_costs.py:5-11, pos_app.py:11-17
Whole Integer Rounding (round_costs.py)
A standalone script, round_costs.py, provides a utility to round all product prices in the database to the nearest whole integer. It uses a standard rounding rule where values of .5 and above are rounded up. This script is intended for bulk data normalization and is not used during the application's real-time operations.
The core logic is in the round_half_up function:
# File: round_costs.py:5-11
def round_half_up(n):
"""
Round using standard rounding rules:
- Numbers with .5 and above round UP to the next whole integer
- Numbers with less than .5 round DOWN to the previous whole integer
"""
return math.floor(n + 0.5)
Sources: round_costs.py:5-11
Decimal-Precise Rounding (pos_app.py)
The main application, pos_app.py, uses a more precise rounding method that handles monetary values to one decimal place. The round_price function leverages Python's Decimal type to avoid the inaccuracies of floating-point arithmetic, implementing a "round half up" strategy. This function is the standard for all price and cost manipulations within the application GUI and data import processes.
The core logic is in the round_price function:
# File: pos_app.py:11-17
def round_price(value):
"""Round monetary values to 1 decimal place using standard rounding (round half up)"""
try:
# Use Decimal for precise rounding to avoid banker's rounding
decimal_value = Decimal(str(float(value)))
return float(decimal_value.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP))
except (ValueError, TypeError, InvalidOperation):
return 0.0
Sources: pos_app.py:11-17
Comparison of Rounding Functions
| Feature | round_half_up (round_costs.py) | round_price (pos_app.py) |
|---|---|---|
| Purpose | Bulk update prices to whole integers. | Real-time rounding of prices and costs. |
| Precision | Rounds to the nearest whole integer. | Rounds to one decimal place. |
| Method | Uses math.floor(n + 0.5). | Uses Decimal.quantize for precision. |
| Usage | Standalone script execution. | Integrated into add_product, update_product, import_from_xlsx. |
Sources: round_costs.py:5-11, 13-50, pos_app.py:11-17, 171-175, 1547-1550
Application and Integration
The rounding utilities are integrated at several key points in the system to ensure data consistency.
Standalone Price Normalization Script
The round_prices function in round_costs.py acts as a command-line tool to iterate through all products in the pos_inventory.db and update their prices to the nearest whole integer. It connects to the database, fetches all products, rounds the price for each, and commits the changes. Finally, it provides a summary and verification of the operation.
The following diagram illustrates the workflow of the round_prices script.
Sources: round_costs.py:13-81
Real-time Rounding in POS Application
The round_price function is consistently applied whenever a product's price or cost is created or modified through the application's user interface or data import features.
- Adding a Product:
db.add_productcallsround_priceon bothpriceandcost. - Updating a Product:
db.update_productcallsround_priceonpriceandcost. - Importing from XLSX: The
import_from_xlsxmethod usesround_priceto clean and standardize cost and price data from the spreadsheet before inserting or updating it in the database. This is crucial for handling potential formatting issues in source files, as seen in import error logs.
Sources: pos_app.py:171-175, 273-277, 1547-1550, error_logs/importing_errors_2025-11-01_14-52-12.txt
One-Time Database Migration
On its first run, the POSApplication executes a one-time database migration named migrate_prices_to_one_decimal. This ensures that all existing price and cost values in the products table are normalized to one decimal place, aligning legacy data with the application's current rounding standard. The migration's execution is tracked in a migrations table to prevent it from running more than once.
The diagram below shows the sequence of the one-time price migration.
Sources: pos_app.py:126-156
Database Schema Impact
The rounding logic directly impacts the products table in the pos_inventory.db SQLite database. While the price and cost columns are defined with the REAL data type, which can store floating-point numbers, the application logic enforces a stricter format.
The diagram below shows the relevant columns in the products table.
This schema is defined during database initialization. The price and cost columns are affected by all rounding operations.
Sources: pos_app.py:25-34, 71-73, CLAUDE.md
Summary
The cost rounding utilities are a fundamental aspect of the application's data management strategy. By employing both a bulk-update script (round_costs.py) and a precise, real-time rounding function (pos_app.py), the system ensures that all financial data related to products is consistent, accurate, and free from common floating-point errors. The one-time migration script further guarantees that data conforms to the one-decimal-place standard, which is critical for reliable sales calculations, reporting, and inventory valuation.
Sources: round_costs.py, pos_app.py