whallysson / money-precision
A simple library for precise monetary value handling and financial calculations
Requires
- php: ^8.1
- ext-bcmath: *
Requires (Dev)
- laravel/pint: ^1.18
- pestphp/pest: ^1.23
- pestphp/pest-plugin: ^1.1
- phpstan/phpstan-strict-rules: ^1.6
- rector/rector: ^1.2
- symfony/var-dumper: ^5.4
- thecodingmachine/phpstan-strict-rules: ^1.0
This package is auto-updated.
Last update: 2024-12-11 16:22:09 UTC
README
MoneyPrecision is a PHP library designed to handle monetary values with precision, ensuring accurate conversions, formatting, and arithmetic operations. With support for multiple currencies, it is ideal for applications that demand reliability in financial calculations.
🚀 Features
-
Conversions between integer and decimal values ​​while maintaining precision.
-
Arithmetic operations (addition, subtraction, multiplication and division) with high accuracy.
-
Comparisons between monetary values.
-
Support for multiple currencies with custom formatting.
-
Intuitive and extensible API.
-
Precise Conversion:
- Integer to Decimal and vice-versa.
- Handles trailing zeros (e.g.,
5660 -> 56.60
).
-
Arithmetic Operations:
- Addition, subtraction, multiplication, and division.
- Guaranteed accuracy with
bcmath
.
-
Currency Support:
- Easily extendable for multiple currencies.
- Includes BRL, USD, and EUR by default.
-
Comparison:
- Compare monetary values using
equals
,greaterThan
, andlessThan
.
- Compare monetary values using
-
Customizable Formatting:
- Flexible formatting with symbols, separators, and positioning.
đź› Installation
Install MoneyPrecision with Composer:
"whallysson/money-precision": "^2.0"
or run
composer require whallysson/money-precision
đź“– Documentation
For detailed information and examples, visit the documentation.
đź“š Usage
Basic Conversions
<?php use Whallysson\Money\Money; echo Money::of(10086)->decimal() . PHP_EOL; // Output: 100.86 echo Money::of(5660)->decimal() . PHP_EOL; // Output: 56.60 echo Money::of(100.86)->int() . PHP_EOL; // Output: 10086 echo Money::of('56.60')->int() . PHP_EOL; // Output: 5660
Arithmetic Operations
<?php use Whallysson\Money\Money; $money1 = Money::of(100.86); $money2 = Money::of(56.60); echo $money1->add($money2)->decimal() . PHP_EOL; // Output: 157.46 echo $money1->sub($money2)->decimal() . PHP_EOL; // Output: 44.26 echo $money1->mul(2)->decimal() . PHP_EOL; // Output: 201.72 echo $money1->div(2)->decimal() . PHP_EOL; // Output: 50.43
Comparisons
<?php use Whallysson\Money\Money; $money1 = Money::of(100.86); $money2 = Money::of(56.60); var_dump([ 'equals' => $money1->equals($money2), // false 'greaterThan' => $money1->greaterThan($money2), // true 'lessThan' => $money1->lessThan($money2), // false ]);
Currency Formatting
<?php use Whallysson\Money\Money; echo Money::of(100.86)->format() . PHP_EOL; // Output: R$ 100,86 echo Money::of(56.60)->format('USD') . PHP_EOL; // Output: $ 56.60 echo Money::of(356.78->format('EUR') . PHP_EOL; // Output: 356,78 €
Contributing
Please see CONTRIBUTING for details.
Support
Security: If you discover any security related issues, please email whallysson.dev@gmail.com instead of using the issue tracker.
Se você descobrir algum problema relacionado à segurança, envie um e-mail para whallysson.dev@gmail.com em vez de usar o rastreador de problemas.
Thank you
Credits
- Whallysson Avelino (Developer)
- Whallysson (Team)
- All Contributors (This Rock)
đź“ť License
The MIT License (MIT). Please see License File for more information.