Makes simple math calculations and format the results.
- Number
Install with composer
composer require leandro47/simple-math- sum
+ - subtraction
- - divider
/ - multiplication
*
use Leandro47\SimpleMath\TypeValue\Number;
$value = Number::create(10);
echo $value->value(); // output 10use Leandro47\SimpleMath\TypeValue\Number;
$value1 = Number::create(10.5);
$value2 = Number::create(10.5);
$result = $value1->sum($value2);
echo $result->value(); // 21$value1 = Number::create(10);
$value2 = Number::create(11);
$result = $value1->subtraction($value2);
echo $result->value(); // -1$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 zerouse Leandro47\SimpleMath\TypeValue\Number;
$value1 = Number::create(2);
$value2 = Number::create(5);
echo $value1->multiplication($value2)->value(); // 10use Leandro47\SimpleMath\Format\NumberFormat;
$decimalSeparator = ',';
$thousandSeparator = '.';
$format = NumberFormat::create($decimalSeparator, $thousandSeparator);
$format->setValue(1000);
echo $format->show(); // "1.000,00"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"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"