granam / number
Converter and wrapping object for a number (float or integer) value
Installs: 23 235
Dependents: 6
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=7.3
- granam/scalar: ^5.0
Requires (Dev)
- granam/exceptions-hierarchy: ^5.0
- granam/test-with-mockery: ^2.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^9.0
README
<?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()); }