PHP code example of nicmart / numbers

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

    

nicmart / numbers example snippets


use Numbers\Number;
$n = new Number(3.1415926535898);
$n = Number::n(3.1415926535898);

var_dump($n->get()); //double(3.1415926535898) 

$n->round(5)->get(); // returns 3.1416

(new Number(0.000123456))->round(5)->get(); // returns 0.00012346

$sciNotation = Number::n(1234.567)->getSciNotation();
echo $sciNotation->significand; // 1.234567
echo $sciNotation->magnitude; // 4

// Prints "1.23k"
echo Number::n(1234.567)->round(3)->getSuffixNotation();


// Prints "79G"
echo Number::n(79123232123)->round(2)->getSuffixNotation();


// Prints "123.4µ"
echo Number::n(0.0001234)->getSuffixNotation();

// Prints "123,123.23"
echo Number::n(123123.23)->format();

// Prints "123 123,23"
echo Number::n(123123.23)->format(',', ' ');

// Returns "123123"
Number::n(123123.23)->floor()->get();

// Returns "123124"
Number::n(123123.23)->ceil()->get();

// Returns 6
Number::n(123123.23)->getMagnitude();

// Returns -2
Number::n(0.01232)->getMagnitude();

// Returns 4
Number::n(1234.5678)->getDigit(0);
// Returns 3
Number::n(1234.5678)->getDigit(1);
//Returns 7
Number::n(1234.5678)->getDigit(-3);

// Second optional arg is the base (default is 10)
// Returns 1
Number::n(bindec('10110101'))->getDigit(0, 2);
// Returns 0
Number::n(bindec('10110101'))->getDigit(6, 2);

// Returns 1
Number::n(12312)->getSign();

// Returns -1
Number::n(-0.0023)->getSign();

// Returns 0
Number::n(0)->getSign();

$double = function($n){ return 2 * $n; };
// Returns 16
Number::n(4)->apply($double)->apply($double)->get();