leandro47 / simple-math
perform math calculations and format the results
v1.0.2
2022-09-10 00:03 UTC
Requires
- php: ^8.1
Requires (Dev)
- phan/phan: ^5.2
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2025-07-10 06:08:29 UTC
README
Makes simple math calculations and format the results.
Type of values
- Number
Installation
Install with composer
composer require leandro47/simple-math
Number
Expressions supported
- sum
+
- subtraction
-
- divider
/
- multiplication
*
Functions
Use/Examples
Creating a number:
use Leandro47\SimpleMath\TypeValue\Number; $value = Number::create(10); echo $value->value(); // output 10
Sum values
use Leandro47\SimpleMath\TypeValue\Number; $value1 = Number::create(10.5); $value2 = Number::create(10.5); $result = $value1->sum($value2); echo $result->value(); // 21
Subtraction values
$value1 = Number::create(10); $value2 = Number::create(11); $result = $value1->subtraction($value2); echo $result->value(); // -1
Divider values
$value1 = Number::create(10); $value2 = Number::create(10); echo $value1->divider($value2)->value(); // 1;
You may get an error when you try to divide with zero, that's why that is necessary
use an try catch block with DivisionByZeroError
.
$value1 = Number::create(10); $value2 = Number::create(0); try { $result = $value1->divider($value2)->value(); } catch (DivisionByZeroError $e) { $result = $e->getMessage(); } echo $result; // Value not to be zero
Multiplication values
use Leandro47\SimpleMath\TypeValue\Number; $value1 = Number::create(2); $value2 = Number::create(5); echo $value1->multiplication($value2)->value(); // 10
Format values
use Leandro47\SimpleMath\Format\NumberFormat; $decimalSeparator = ','; $thousandSeparator = '.'; $format = NumberFormat::create($decimalSeparator, $thousandSeparator); $format->setValue(1000); echo $format->show(); // "1.000,00"
Precision
If you want to add decimal places just add an extra parameter $precision
.
use Leandro47\SimpleMath\Format\NumberFormat; $decimalSeparator = ','; $thousandSeparator = '.'; $precision = 4; $format = NumberFormat::create($decimalSeparator, $thousandSeparator, $precision); echo $format->setValue(1000)->show(); // "1.000,0000" echo $format->setValue(1000.45895)->show(); // "1.000,4590"
Symbol
You can also add a prefix before the number just add an extra parameter $symbol
.
use Leandro47\SimpleMath\Format\NumberFormat; $decimalSeparator = ','; $thousandSeparator = '.'; $precision = 2; $symbol = 'R$'; $format = NumberFormat::create($decimalSeparator, $thousandSeparator, $precision, $symbol); echo $format->setValue(1000.5)->show(); // "R$ 1.000,50"
You can add the format when you show the result of some calculation.
use Leandro47\SimpleMath\Format\NumberFormat; use Leandro47\SimpleMath\TypeValue\Number; $decimalSeparator = ','; $thousandSeparator = '.'; $precision = 2; $symbol = 'R$'; $format = NumberFormat::create($decimalSeparator, $thousandSeparator, $precision, $symbol); $value1 = Number::create(10.5, $format); echo $value1->format(); // "R$ 10.50" echo $format->setValue(1000.5)->show(); // "R$ 1.000,50"
or
$decimalSeparator = ','; $thousandSeparator = '.'; $precision = 2; $symbol = 'R$'; $format = NumberFormat::create($decimalSeparator, $thousandSeparator, $precision, $symbol); $value1 = Number::create(10.5); $value2 = Number::create(1000.5); echo $value1->multiplication($value2)->format($format); // "R$ 10.505,25"