24

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.

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

4
  • 3
    Works just fine for me ? Commented May 12, 2013 at 18:26
  • 1
    It's no fine for me :'( Commented May 12, 2013 at 18:37
  • Did you try (int), or do you actually need a float ? Commented May 12, 2013 at 18:42
  • (int) in 32-bit system will cause overflow. Max value is 2147483647. Commented Jun 3, 2014 at 15:03

4 Answers 4

22

Surprisingly there is no accepted answer. The issue only exists in 32-bit 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.

In other words, the $string is first interpreted as INT, which cause overflow (The $string value 2968789218 exceeds the maximum value (PHP_INT_MAX) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float) or 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:

echo PHP_INT_MAX;

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

Sign up to request clarification or add additional context in comments.

1 Comment

It's not entirely clear. Is it necessary to store the floatval result in a variable instead of directly calling the function in echo?
9

$float = floatval($string);

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

Comments

5

Try using

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

7 Comments

@tegaralaga How are you checking result? The .00 looks fishy.
(double) and (real) does'nt really exist in PHP, so casting to those will cast to (float) which is exactly what the OP is doing..
looking ok at my end. what platform you are using. Read more about float.
Is there something wrong with my server? I use free server from RedHat OpenShift frodo-baggins.rhcloud.com/info.htm
sorry but i am unable to figure out what causing this.
|
0

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

    $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 ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.