PHP code example of granam / float

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

    

granam / float example snippets



use Granam\Float\FloatObject;

$float = new FloatObject(123.456);

// double(123.456)
var_dump($float->getValue());
// string(7) "123.456"
var_dump((string)$float);

$float = new FloatObject(null);
// double(0)
var_dump($float->getValue());
// string(0)
var_dump((string)$float);

$float = new FloatObject($withTooLongDecimal = '123456.999999999999999999999999999999999999');
// double 123457
var_dump($float->getValue());

try {
  new FloatObject('123.999999999999999999999999999999', true /* paranoid to rounding */);
} catch (\Granam\Float\Tools\Exceptions\WrongParameterType $floatException) {
  // Something get wrong: Some value has been lost on cast. Given string-number '123456.999999999999999999999999999999999999' results into float 123457
  die('Something get wrong: ' . $floatException->getMessage());
}