Download the PHP package aichadigital/lara100 without Composer
On this page you can find all versions of the php package aichadigital/lara100. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aichadigital/lara100
More information about aichadigital/lara100
Files in aichadigital/lara100
Package lara100
Short Description Laravel cast for handling decimal values as base-100 integers (cents/centesimals)
License MIT
Homepage https://github.com/aichadigital/lara100
Informations about the package lara100
Lara100 - Base-100 Cast for Laravel
A Laravel package that provides a custom Eloquent cast for handling monetary/decimal values by storing them as integers (cents) in the database while working with decimals in your PHP code, eliminating floating-point precision errors.
Why Lara100?
Floating-point arithmetic in PHP (and most programming languages) can lead to precision errors:
This is particularly problematic when dealing with:
- 💰 Monetary values (prices, amounts, balances)
- 📊 Percentages (tax rates, discounts)
- 📏 Any centesimal measurements
Lara100 solves this by storing values as integers (cents) in the database, while letting you work with familiar decimal values in your code.
Installation
You can install the package via Composer:
Configuration (Optional)
You can optionally publish the configuration file:
This will create a config/lara100.php file where you can configure:
- Rounding mode (default:
PHP_ROUND_HALF_UP) - BCMath usage (default:
false)
Alternatively, you can set these via environment variables in your .env:
Requirements
- PHP 8.3 or 8.4
- Laravel 11.x or 12.x
- Optional: BCMath extension (for high-precision calculations)
Usage
Basic Usage (Cast)
Apply the Base100 cast to your model attributes:
Now you can work with decimals in your application while storing integers in the database:
Advanced Usage (Trait)
For convenience, you can use the HasBase100 trait to apply the cast to multiple attributes at once:
The trait automatically applies the Base100 cast to all specified attributes.
How It Works
Database → Application (GET)
Application → Database (SET)
Rounding Behavior
By default, the cast uses Round Half Up (PHP_ROUND_HALF_UP):
0.555→0.56(rounds up when exactly halfway)0.554→0.55
This is the standard rounding mode used in Spain, the EU, and most countries.
Configuring Rounding Mode
You can configure the rounding mode globally via config or per-attribute:
Global configuration (affects all casts):
Per-attribute override (in your model):
Available Rounding Modes
| Mode | Constant | Behavior | Use Case |
|---|---|---|---|
| Half Up | PHP_ROUND_HALF_UP (1) |
0.555→0.56, 0.545→0.55 | Spain/EU standard |
| Half Even | PHP_ROUND_HALF_EVEN (3) |
0.555→0.56, 0.545→0.54 | Accounting (Banker's) |
| Half Down | PHP_ROUND_HALF_DOWN (2) |
0.555→0.55, 0.545→0.54 | Conservative rounding |
| Half Odd | PHP_ROUND_HALF_ODD (4) |
0.555→0.55, 0.545→0.55 | Specialized cases |
BCMath Support
For maximum precision with very large amounts, enable BCMath:
Or per-attribute:
Note: Requires the bcmath PHP extension to be installed.
Examples
Working with Monetary Values
Performing Calculations
Handling Edge Cases
Database Schema
Your database columns should be defined as INTEGER (to store cents):
Why INTEGER instead of DECIMAL?
- ✅ Better performance (integer operations are faster)
- ✅ Less storage space
- ✅ No floating-point precision issues at database level
- ✅ Compatible with all database engines
Comparison with Alternatives
| Solution | DB Column Type | PHP Value Type | Precision | Package Size |
|---|---|---|---|---|
| Lara100 | INTEGER (cents) |
float (19.99) |
✅ Perfect | Lightweight cast |
| moneyphp/money | INTEGER (cents) |
Money object |
✅ Perfect | Full-featured library |
| brick/money | INTEGER (cents) |
Money object |
✅ Perfect | Full-featured library |
| Native DECIMAL | DECIMAL(10,2) |
float (19.99) |
⚠️ Precision issues | No package needed |
Key Differences:
- Lara100: Lightweight cast that stores integers in DB (efficient), but lets you work with decimals in PHP (natural)
- Money libraries: Full-featured libraries with objects, currency conversion, formatting, etc.
- Native DECIMAL: Traditional approach, works with floats in PHP (precision issues)
Choose Lara100 when:
- ✅ You want a simple, Laravel-native solution
- ✅ You prefer working with familiar decimal values (19.99)
- ✅ You want efficient integer storage in the database
- ✅ You don't need currency conversions or complex money operations
- ✅ You want to avoid float precision errors without heavy dependencies
Consider alternatives when:
- ❌ You need multi-currency support
- ❌ You need complex monetary operations (allocation, distribution, rounding strategies)
- ❌ You prefer working with Money objects instead of scalar decimals
- ❌ You need advanced formatting and localization features
Testing
The package includes comprehensive tests for both the cast and trait:
Run tests with coverage:
Run tests in parallel:
Code Quality
Run PHPStan static analysis:
Run Laravel Pint code formatter:
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Security Vulnerabilities
If you discover a security vulnerability, please send an e-mail to Abdelkarim Mateos Sanchez via [email protected].
Credits
- Abdelkarim Mateos Sanchez
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
About AichaDigital
AichaDigital is a ITt company focused on IT services.
All versions of lara100 with dependencies
illuminate/contracts Version ^12.0||^13.0
illuminate/database Version ^12.0||^13.0
spatie/laravel-package-tools Version ^1.16