PHP 将字符串转换为浮点/双精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16510636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:15:33  来源:igfitidea点击:

PHP Convert String into Float/Double

phpstringfloating-pointimplicit-conversion

提问by Bias Tegaralaga

I have list of string (size in bytes), I read those from file. Let say one of the string is 2968789218, but when I convert it to float it become 2.00.

我有字符串列表(以字节为单位的大小),我从文件中读取了它们。假设其中一个字符串是 2968789218,但是当我将其转换为浮点数时,它变为 2.00。

This is my code so far :

到目前为止,这是我的代码:

$string = "2968789218";
$float = (float)$string;
//$float = floatval($string);
//the result is same
// result 2.00

Anyone?

任何人?

Solved

解决了

The problem was actually the encoding. It's fine now when I change the file encoding :D

问题实际上是编码。当我更改文件编码时现在很好:D

采纳答案by Raptor

Surprisingly there is no accepted answer. The issue only exists in 32-bit PHP.

令人惊讶的是,没有公认的答案。该问题仅存在于 32 位 PHP 中。

From the documentation,

文档中

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

如果字符串不包含任何字符 '.'、'e' 或 'E' 并且数值符合整数类型限制(由 PHP_INT_MAX 定义),则该字符串将被评估为整数。在所有其他情况下,它将被评估为浮点数。

In other words, the $stringis first interpreted as INT, which cause overflow (The $stringvalue 2968789218exceeds the maximum value (PHP_INT_MAX) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float)or floatval().

换句话说,$string首先将the解释为 INT,导致溢出($string2968789218超过PHP_INT_MAX32 位 PHP的最大值 ( ),即2147483647。),然后评估为浮点数(float)floatval()

Thus, the solution is:

因此,解决方案是:

$string = "2968789218";
echo 'Original: ' . floatval($string) . PHP_EOL;
$string.= ".0";
$float = floatval($string);
echo 'Corrected: ' . $float . PHP_EOL;

which outputs:

输出:

Original: 2.00
Corrected: 2968789218

To check whether your PHP is 32-bit or 64-bit, you can:

要检查您的 PHP 是 32 位还是 64 位,您可以:

echo PHP_INT_MAX;

If your PHP is 64-bit, it will print out 9223372036854775807, otherwise it will print out 2147483647.

如果你的 PHP 是 64 位的,它会打印出9223372036854775807,否则它会打印出2147483647.

回答by Muya Enzo

$float = floatval($string);

$float = floatval($string);

Ref : http://www.php.net/floatval

参考:http: //www.php.net/floatval

回答by Sid

Try using

尝试使用

$string = "2968789218";
$float = (double)$string;

回答by daarksim

If the function floatval does not work you can try to make this :

如果函数 floatval 不起作用,您可以尝试这样做:

    $string = "2968789218";
    $float = $string * 1.0;
    echo $float;

But for me all the previous answer worked ( try it in http://writecodeonline.com/php/) Maybe the problem is on your server ?

但是对我来说,以前的所有答案都有效(在http://writecodeonline.com/php/ 中尝试)也许问题出在您的服务器上?