PHP code example of leandro47 / simple-math

1. Go to this page and download the library: Download leandro47/simple-math library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leandro47 / simple-math example snippets


use Leandro47\SimpleMath\TypeValue\Number;

$value = Number::create(10);
echo $value->value(); // output 10

use 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;

$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


use Leandro47\SimpleMath\TypeValue\Number;

$value1 = Number::create(2);
$value2 = Number::create(5);

echo $value1->multiplication($value2)->value(); // 10

use Leandro47\SimpleMath\Format\NumberFormat;

$decimalSeparator = ',';
$thousandSeparator = '.';

$format = NumberFormat::create($decimalSeparator, $thousandSeparator);
$format->setValue(1000);
echo $format->show(); // "1.000,00"

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"

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"

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"

$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"