PHP code example of utecca / number

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

    

utecca / number example snippets


// Init via constructor
new Number(123.456);

// Init via static method
Number::of('123.45');

// Force a certain number of decimals
Number::of('123.45', 1); // Will return 123.5

protected $casts = [
    // Amount with two decimals (usually used for monetary values) 
    'amount' => Utecca\Number\Casts\NumberFromDecimal::class,
    // Amount with a custom number of decimals
    'quantity' => Utecca\Number\Casts\NumberFromDecimal::class . ':4'),
];

// Addition
$number->add(100);

// Subtraction
$number->sub(100);

// Multiplication
$number->mul(100);

// Division
$number->div(100);

// Percentage
$number->percent(50);

// Round
$number->round(2);

// Floor
$number->floor();

// Ceil
$number->ceil();

// Absolute
$number->abs();


// Various getters
$number->isZero();
$number->isPositive();
$number->isPositiveOrZero();
$number->isNegative();
$number->isNegativeOrZero();
$number->lt(100);
$number->lte(100);
$number->gt(100);
$number->gte(100);
$number->eq(100);

// Various formatters
$number->toString();
$number->toInt();
$number->inCents(); // Only works if the number has two decimals or less