PHP code example of filmtools / mround
1. Go to this page and download the library: Download filmtools/mround 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/ */
filmtools / mround example snippets
use function FilmTools\MRounder\mround;
use function FilmTools\MRounder\mfloor;
use function FilmTools\MRounder\mceil;
use function FilmTools\MRounder\mround;
echo mround( 12, 10); // 10
echo mround( 2.4, 0.5); // 2.5
echo mround( 11.2, 1/3); // 11.333333333333
echo mround( 11.1, 1/3); // 11
use function FilmTools\MRounder\mfloor;
echo mfloor( 59, 10); // 50
echo mfloor( 2.4, 0.5); // 2.0
use function FilmTools\MRounder\mceil;
echo mceil( 51, 10); // 60
echo mceil( 2.4, 0.5); // 2.5
use FilmTools\MRounder\MRounder;
// Instantiate with the desired base multiple
$mrounder = new MRounder( 0.5 );
$mrounder = new MRounder( 0.5, MRounder::ROUND );
$mrounder = new MRounder( 0.5, "round" );
echo $mrounder( 2.4 ); // 2.5
// Down-rounder
$round_down = new MRounder( 0.5, MRounder::FLOOR );
$round_down = new MRounder( 0.5, "floor" );
echo $round_down( 2.4 ); // 2.0
// Up-rounder
$round_up = new MRounder( 0.5, MRounder::CEIL );
$round_up = new MRounder( 0.5, "ceil" );
echo $round_up( 7.2 ); // 7.5
// Bonus – You will find this interesting:
echo mround( 99, 0); // 0
// Build an array with equal keys and values:
$steps = range(0, 1, 0.1);
$numbers = array_combine($steps, $steps);
// Now let's round to multiples
// of one-sixth fraction:
$mround = new MRounder( 1/6 );
$sixths = array_map($mround, $numbers);
print_r($sixths);
// Output:
Array
(
[0] => 0
[0.1] => 0.16666666666667
[0.2] => 0.16666666666667
[0.3] => 0.33333333333333
[0.4] => 0.33333333333333
[0.5] => 0.5 # funny, but of course equals 2/6.
[0.6] => 0.66666666666667
[0.7] => 0.66666666666667
[0.8] => 0.83333333333333
[0.9] => 0.83333333333333
[1] => 1
)
use FilmTools\MRounder\MRounder;
use FilmTools\MRounder\MRoundInvalidArgumentException;
use FilmTools\MRounder\MRoundExceptionInterface;
try {
$mround = new MRounder( "foobar" );
// accordingly
echo mround( 22, "string");
echo mround( "foo", 4);
}
catch (MRoundExceptionInterface $e) {
echo get_class( $e );
echo $e->getMessage();
// MRoundInvalidArgumentException
// Parameter must be numeric.
}