Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Float_.php

function Float_::parse

@internal

Parses a DNUMBER token like PHP would.

Parameters

string $str A string number:

Return value

float The parsed number

1 call to Float_::parse()
Float_::fromString in vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Float_.php

File

vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Float_.php, line 45

Class

Float_

Namespace

PhpParser\Node\Scalar

Code

public static function parse(string $str) : float {
    $str = str_replace('_', '', $str);
    // Check whether this is one of the special integer notations.
    if ('0' === $str[0]) {
        // hex
        if ('x' === $str[1] || 'X' === $str[1]) {
            return hexdec($str);
        }
        // bin
        if ('b' === $str[1] || 'B' === $str[1]) {
            return bindec($str);
        }
        // oct, but only if the string does not contain any of '.eE'.
        if (false === strpbrk($str, '.eE')) {
            // substr($str, 0, strcspn($str, '89')) cuts the string at the first invalid digit
            // (8 or 9) so that only the digits before that are used.
            return octdec(substr($str, 0, strcspn($str, '89')));
        }
    }
    // dec
    return (double) $str;
}
RSS feed
Powered by Drupal