kazuto/laravel-money

Money Cast and Facade for Laravel

1.2.1 2022-02-12 16:41 UTC

This package is auto-updated.

Last update: 2024-11-12 22:46:22 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package adds a money cast and money facade to simplify storing financial values in the database by adding them as an integer column instead of float, double, or decimal, thus eliminating the possibility of floating point errors and resulting miscalculations.

Installation

You can install the package via composer:

composer require kazuto/laravel-money

You can publish and run the migrations with:

php artisan vendor:publish --provider="Kazuto\LaravelMoney\MoneyServiceProvider"

You can publish the config file with:

php artisan vendor:publish --provider="Kazuto\LaravelMoney\MoneyServiceProvider" --tag="money-config"

This is the contents of the published config file:

return [
  'locale_iso' => 'en_US'
];

Usage

// Base
Money::fromInt(524)->toInt();   // 524
Money::fromInt(524)->toFloat(); // 5.24
Money::fromInt(524)->toText();  // $5.24
Money::fromInt(524)->toArray();
// [
//   'value' => 524,
//   'formatted' => '$5.24',
//   'currency' => 'USD',
//   'symbol' => '$',
// ]

Money::fromFloat(5.24)->toInt();    // 524
Money::fromFloat(5.24)->toFloat();  // 5.24
Money::fromFloat(5.24)->toText();   // $5.24
Money::fromFloat(5.24)->toArray();
// [
//   'value' => 524,
//   'formatted' => '$5.24',
//   'currency' => 'USD',
//   'symbol' => '$',
// ] 


// Math

// add(Money|int|float)
Money::fromInt(524)->add(123);                    // 647
Money::fromInt(524)->add(1.23);                   // 647
Money::fromInt(524)->add(Money::fromFloat(1.23)); // 647

// substract(Money|int|float)
Money::fromInt(524)->substract(123);                    // 401
Money::fromInt(524)->substract(1.23);                   // 401
Money::fromInt(524)->substract(Money::fromFloat(1.23)); // 401

// multiply(int|float)
Money::fromInt(524)->multiply(2);     // 1048
Money::fromInt(524)->multiply(1.23);  // 645 (rounded from 644.52)

// divide(int|float)
Money::fromInt(524)->divide(2);       // 262
Money::fromInt(524)->divide(1.23);    // 426 (rounded from 426.01)

// Comparisons

// isEqualTo(Money|int|float)
Money::fromInt(524)->isEqualTo(524);                    // true
Money::fromInt(524)->isEqualTo(Money::fromFloat(5.24)); // true
Money::fromInt(524)->isEqualTo(1.23);                   // false
Money::fromInt(524)->isEqualTo(Money::fromInt(123));    // false

// isGreaterThan(Money|int|float)
Money::fromInt(524)->isGreaterThan(123);                    // true
Money::fromInt(524)->isGreaterThan(Money::fromFloat(1.23)); // true
Money::fromInt(524)->isGreaterThan(8.58);                   // false
Money::fromInt(524)->isGreaterThan(Money::fromInt(858));    // false
Money::fromInt(524)->isGreaterThan(524);                    // false
Money::fromInt(524)->isGreaterThan(Money::fromFloat(5.24)); // false

// isGreaterThanOrEqual(Money|int|float)
Money::fromInt(524)->isGreaterThanOrEqual(8.58);                   // false
Money::fromInt(524)->isGreaterThanOrEqual(524);                    // true
Money::fromInt(524)->isGreaterThanOrEqual(Money::fromFloat(5.24)); // true

// isLessThan(Money|int|float)
Money::fromInt(524)->isLessThan(8.58);                   // true
Money::fromInt(524)->isLessThan(Money::fromInt(858));    // true
Money::fromInt(524)->isLessThan(123);                    // false
Money::fromInt(524)->isLessThan(Money::fromFloat(1.23)); // false
Money::fromInt(524)->isLessThan(524);                    // false
Money::fromInt(524)->isLessThan(Money::fromFloat(5.24)); // false

// isLessThanOrEqual(Money|int|float)
Money::fromInt(524)->isLessThanOrEqual(2.24);                   // false
Money::fromInt(524)->isLessThanOrEqual(524);                    // true
Money::fromInt(524)->isLessThanOrEqual(Money::fromFloat(5.24)); // true

Testing

# Unit Tests
composer test:run

# Coverage
composer test:coverage

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.