granam/number

Converter and wrapping object for a number (float or integer) value

Installs: 22 935

Dependents: 6

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:project

5.1.0 2021-03-24 09:27 UTC

README

Build Status

<?php
use Granam\Number\NumberObject;
use \Granam\Number\Tools\Exceptions\WrongParameterType;

$stringFloatToNumber = new NumberObject('123.456');
var_dump($stringFloatToNumber->getValue()); // double(123.456)
var_dump((string)$stringFloatToNumber); // string(7) "123.456"

$stringIntToNumber = new NumberObject('123');
var_dump($stringIntToNumber->getValue()); // int(123)
var_dump((string)$stringIntToNumber); // string(7) "123"

$nullToNumber = new NumberObject(null);
var_dump($nullToNumber->getValue()); // int(0)
var_dump((string)$nullToNumber); // string(1) "0"

$tooLongDecimalToNumber = new NumberObject($withTooLongDecimal = '123456.999999999999999999999999999999999999');
var_dump($tooLongDecimalToNumber->getValue());// int(123457); because of intval(floatval($value))

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