3 namespace PhpParser\Node\Scalar;
5 use PhpParser\Node\Scalar;
7 class DNumber extends Scalar
9 /** @var float Number value */
13 * Constructs a float number scalar node.
15 * @param float $value Value of the number
16 * @param array $attributes Additional attributes
18 public function __construct($value, array $attributes = array()) {
19 parent::__construct($attributes);
20 $this->value = $value;
23 public function getSubNodeNames() {
24 return array('value');
30 * Parses a DNUMBER token like PHP would.
32 * @param string $str A string number
34 * @return float The parsed number
36 public static function parse($str) {
37 // if string contains any of .eE just cast it to float
38 if (false !== strpbrk($str, '.eE')) {
42 // otherwise it's an integer notation that overflowed into a float
43 // if it starts with 0 it's one of the special integer notations
44 if ('0' === $str[0]) {
46 if ('x' === $str[1] || 'X' === $str[1]) {
51 if ('b' === $str[1] || 'B' === $str[1]) {
56 // substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit (8 or 9)
57 // so that only the digits before that are used
58 return octdec(substr($str, 0, strcspn($str, '89')));