PHP code example of tiny-blocks / math

1. Go to this page and download the library: Download tiny-blocks/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/ */

    

tiny-blocks / math example snippets


BigDecimal::fromString(value: '10');
BigDecimal::fromString(value: '-123.456');

BigDecimal::fromString(value: '10', scale: 2);

BigDecimal::fromFloat(value: 10.0);
BigDecimal::fromFloat(value: -123.456);

BigDecimal::fromFloat(value: 10.0, scale: 2);

$augend = BigDecimal::fromString(value: '1');
$addend = BigDecimal::fromFloat(value: 1.0);

$result = $augend->add(addend: $addend);

$result->toString(); # 2

$minuend = BigDecimal::fromString(value: '1');
$subtrahend = BigDecimal::fromFloat(value: 1.0);

$result = $minuend->subtract(subtrahend: $subtrahend);

$result->toString(); # 0

$multiplicand = BigDecimal::fromString(value: '1');
$multiplier = BigDecimal::fromFloat(value: 1.0);

$result = $multiplicand->multiply(multiplier: $multiplier);

$result->toString(); # 1

$dividend = BigDecimal::fromString(value: '1');
$divisor = BigDecimal::fromFloat(value: 1.0);

$result = $dividend->divide(divisor: $divisor);

$result->toString(); # 1

    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
    
    $result = $value->withRounding(mode: RoundingMode::HALF_UP);
    
    $result->toString(); # 1
    

    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
  
    $result = $value->withRounding(mode: RoundingMode::HALF_DOWN);
  
    $result->toString(); # 0.99
    

    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
  
    $result = $value->withRounding(mode: RoundingMode::HALF_EVEN);
  
    $result->toString(); # 1
    

    $value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
  
    $result = $value->withRounding(mode: RoundingMode::HALF_ODD);
  
    $result->toString(); # 0.99