PHPackages                             enimiste/l5-math - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. enimiste/l5-math

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

enimiste/l5-math
================

Laravel 5 Math calculation, number representation package

v1.1.1(9y ago)01802MITPHPPHP &gt;=5.5

Since Jul 21Pushed 9y ago1 watchersCompare

[ Source](https://github.com/enimiste/l5-math)[ Packagist](https://packagist.org/packages/enimiste/l5-math)[ RSS](/packages/enimiste-l5-math/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (4)Used By (2)

Financial calculation in a laravel 5 project
============================================

[](#financial-calculation-in-a-laravel-5-project)

This package is suited for you if you want to build a Laravel 5 application that manage financial numbers (tva, prices, ...). PHP float numbers are not exact due to the floating part.

Float Number
------------

[](#float-number)

For decimal numbers you should use the `FloatNumber` class.

Helpers :
---------

[](#helpers-)

*calculator*Get the calculator instance :

```
 $calculator = calculator();
 $calculator->add(.. , ...);
 ....
```

*check\_is\_integer*Check if a given is integer or not. The `is_int` of php those not check the integer value given as strings

*as\_float\_number*Convert a given value to an instance of `FloatNumber`. Default scale is 2

Usage :
-------

[](#usage-)

Add the service provider and the facade to the `config/app.php` file :

```
    Enimiste\L5Math\Providers\L5MathServiceProvider::class;
```

```
    'Calculator' => 'Enimiste\L5Math\Facades\CalculatorFacade::class;
```

You can access the calculator with two ways :

- `Calculator` facade class
- `calculator()` helper function

Add model mutators :

For fields that you want to convert them and forward from `FloatNumber` to `DB decimal` types you should add :

```
   public function set[Yourattribute]Attribute($value){
        $this->attribute['price'] = as_float_number($value);//__toString() function of VONumber is used to passe data to db
   }

   public function get[Yourattribute]Attribute($value){
        return as_float_number($value);
   }
```

Calculator features :
---------------------

[](#calculator-features-)

```
interface Calculator {

	/**
	 * Multiplication
	 *
	 * @param Number $l
	 * @param Number $r
	 *
	 * @return Number
	 */
	public function mult( Number $l, Number $r );

	/**
	 * Calculate the TTC price from HT and TVA
	 *
	 * @param FloatNumber $ht
	 * @param FloatNumber $tva between 0 and 1
	 *
	 * @return FloatNumber
	 */
	public function ttc( FloatNumber $ht, FloatNumber $tva );

	/**
	 * Add two Numbers
	 *
	 * @param Number $l
	 * @param Number $r
	 *
	 * @return Number
	 */
	public function add( Number $l, Number $r );

	/**
	 * @param IntegerNumber $quantite
	 * @param FloatNumber   $prixUnitaireHt
	 * @param FloatNumber   $tva
	 *
	 * @return PriceResultDto
	 */
	public function price( IntegerNumber $quantite, FloatNumber $prixUnitaireHt, FloatNumber $tva );

	/**
	 * Build TVA as value betwenn 0 and 1 from a value from 0 to 100
	 *
	 * @param FloatNumber $tva
	 *
	 * @return FloatNumber
	 */
	public function tva( FloatNumber $tva );

	/**
	 * Sub two Numbers
	 * $l - $r
	 *
	 * @param Number $l
	 * @param Number $r
	 *
	 * @return Number
	 */
	public function sub( Number $l, Number $r );
}
```

Example :
---------

[](#example-)

Assume we have a `Product` and an `Ordre` entities that we have to manage in our application.

#### Migrations

[](#migrations)

```
   //products table
   $table->increments('id');
   $table->string('title')->unique();
   $table->decimal('price', 8, 2);//000000.00
   $table->decimal('tva', 5, 2)->default(20.0);//between 0.00 and 100.00

   //orders table
   $table->increments('id');
   $table->unsignedInteger('amount');//count of product ordered
   $table->decimal('total_ht', 8, 2);// amount * price
   $table->decimal('total_ttc', 8, 2);
   $table->unsignedInteger('product_id');
```

#### Models

[](#models)

```
class Product extends Model {

   protected $fillable = ['title', 'price', 'tva'];

   public function setPriceAttribute($value){
        $this->attribute['price'] = as_float_number($value);
   }

   public function getPriceAttribute($value){
        return as_float_number($value);
   }

   public function setTvaAttribute($value){
       $this->attribute['tva'] = as_float_number($value);
   }

   public function getTvaAttribute($value){
       return as_float_number($value);
   }

}
```

```
class Order extends Model {

   protected $fillable = ['amount', 'total_ht', 'total_ttc', 'product_id'];

   public function setTotalHtAttribute($value){
        $this->attribute['total_ht'] = as_float_number($value);
   }

   public function getTotalHtAttribute($value){
        return as_float_number($value);
   }

   public function setTotalTtcAttribute($value){
        $this->attribute['total_ttc'] = as_float_number($value);
   }

   public function getTotalTtcAttribute($value){
       return as_float_number($value);
   }

}
```

### Seed

[](#seed)

```
 $p = Product::create(
        [
            'price' =>  100.76,
            'tva'   =>  20.0
        ]
 );

 $calc = calculator();

 $o1 = Order::create(
        [
            'product_id'    =>  $p->id,
            'amount'        =>  33,
            'total_ht'      =>  $ht = $calc->mult($p->price, new Enimiste\Math\VO\IntegerNumber(33)),
            'total_ttc'     =>  $calc->ttc($ht, $calc->tva($p->tva))
        ]
 );

//NB : $calc->tva convert a tva as % to float value between 0 and 1
//another way

 $price = $calc->price(new Enimiste\Math\VO\IntegerNumber(78), $p->price, $calc->tva($p->tva));
 $o2 = Order::create(
        [
            'product_id'    =>  $p->id,
            'amount'        =>  $price->quantite,
            'total_ht'      =>  $price->ht,
            'total_ttc'     =>  $price->ttc
        ]
 );
```

#### Echo 1:

[](#echo-1)

```
 $products = Product::all();

 foreach($products as $p){
    echo 'Price : $' . $p->price->__toString() . PHP_EOL;
    echo 'Tva : ' . $p->tva->__toString() . '%' . PHP_EOL;
 }
```

Will output :

```
 Price : $100.76
 Tva : 20.00%

```

#### Echo 2:

[](#echo-2)

```
 $orders = Order::all();

 foreach($orders as $o){
    echo 'Amount : $' . $o->amount . PHP_EOL;
    echo 'Total Ht : $' . $o->total_ht->__toString() . PHP_EOL;
    echo 'Total Ttc : ' . $o->total_ttc->__toString() . '%' . PHP_EOL;
    echo PHP_EOL;
 }
```

Will output :

```
 Amount : 33
 Total Ht : $3 325.08
 Total Ttc : $3 990.10//exact is 3 990.096 but the scale is 2 sà 0.096 become 0.10

 Amount : 78
 Total Ht : $7 859.28
 Total Ttc : $9 431.14//exact is 9 431.136 but the scale is 2 sà 0.136 become 0.14

```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~93 days

Total

3

Last Release

3445d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.6

v1.1.1PHP &gt;=5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/43e7310829d4711c88c34476daec986374515494fa1db2893f33d9091cc25a1a?d=identicon)[nouni.elbachir](/maintainers/nouni.elbachir)

---

Top Contributors

[![enimiste](https://avatars.githubusercontent.com/u/2515677?v=4)](https://github.com/enimiste "enimiste (10 commits)")

---

Tags

laravelmathnumberfloatpriceTVAfinancial

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/enimiste-l5-math/health.svg)

```
[![Health](https://phpackages.com/badges/enimiste-l5-math/health.svg)](https://phpackages.com/packages/enimiste-l5-math)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[vinkius-labs/laravel-page-speed

Laravel Page Speed

2.5k9.6k1](/packages/vinkius-labs-laravel-page-speed)[emargareten/inertia-modal

Inertia Modal is a Laravel package that lets you implement backend-driven modal dialogs for Inertia apps.

90128.1k](/packages/emargareten-inertia-modal)[linkxtr/laravel-qrcode

A clean, modern, and easy-to-use QR code generator for Laravel

3614.9k](/packages/linkxtr-laravel-qrcode)[wearepixel/laravel-cart

A cart implementation for Laravel

1355.6k](/packages/wearepixel-laravel-cart)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
