开发者

does php have a function that parses exponents in strings (eg: "9.5367431640625e-05") or does it have to be done manually via regexp etc?

开发者 https://www.devze.com 2023-04-01 23:12 出处:网络
does php have a function that will convert a string (data type will be string since its parsed from an external source) like:

does php have a function that will convert a string (data type will be string since its parsed from an external source) like:

string(..) "9.5367431640625e-05"

and perform the exponent calculations on said string and return the calculated value?

or do i need to use regexp etc style way to do something like:

9.5367431640625e-05 -> 9.5367431640625^-5 = calculated value

if php does not have such a functi开发者_运维问答on, best ideas on how to accomplish this? i was just going to pattern match something like

/^([0-9\.\-]+)e([0-9\.\-]+)$/

any better ideas?

thanks!


You could do a simple cast:

$float = (float)$string;

But besides checking for 0 (which might be a valid value ...) there is no way for error checking. You could also use the floatval() function which does the same.

But usually you won't have to do it as PHP will cast as needed. Mind that chapter of the documentation: http://de.php.net/manual/en/language.types.type-juggling.php


Do a cast:

$float  = (float) $string;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号