0

Ok, so I have this float number :-

$floatval = '1.05143617E+18';

It's equivalent integer is :-

1051436170000000000

Using php, I'm trying to convert this float number to the required integer value.

Here is my try :-

$floatval = '1.05143617E+18';
var_dump(convert($floatval));

function convert($floatval)
{
    $divided = explode('+', $floatval);
    $first = floatval($divided[0]);
    $second = intval($divided[1]);
    $final = intval($first * pow(10, $second));
    return $final;
}

Output :-

953738112

Whatever I try, the output is not coming as required. Where am I doing mistake?

4 Answers4

0

Try this

intval(floatval($floatval));
Buddy
  • 10,874
  • 5
  • 41
  • 58
Forbs
  • 1,256
  • 1
  • 7
  • 9
0

It appears you are dealing with integer overflow, probably because you are running PHP on Windows and your system does not support 64 bit integers. You can avoid this issue by doing your calculations with functions from the bcmath extension. If you are running on Windows, PHP will have built-in support for bcmath as noted in the documentation here. Using bcmath, Your convert function could be implemented like this:

function convert($floatval)
{
    $divided = explode('E+', $floatval);
    $base = $divided[0];
    $exp = $divided[1];
    $final = bcmul($base, bcpow('10', $exp));
    return $final;
}

On a different system where you did not have the integer overflow problem, one of the other cast-based solutions should work.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
-1
$floatval = (float) '1.05143617E+18';
$intval = (int) $floatval;
var_dump($intval);

Output: int(1051436170000000000)

Hydra IO
  • 1,537
  • 1
  • 13
  • 28
-2

(int)(float)$floatval; does the trick.

jedifans
  • 2,287
  • 1
  • 13
  • 9
  • Unsure why I'm being downvoted. If you want your specific solution to work, explode on the E rather than the +. Neither solution will work past INT_MAX anyway! – jedifans Aug 18 '16 at 22:29
  • Are you on a 32 bit system? – jedifans Aug 18 '16 at 22:30
  • `var_dump((int)(float)'1.05143617E+18');` on http://sandbox.onlinephpfunctions.com/ gives `int(1051436170000000000)` – jedifans Aug 18 '16 at 22:34
  • @RobertPuerco what do you get on your system? I have a feeling you are running 32bit, where INT_MAX is lower than the 64bit systems most servers run these days. – jedifans Aug 18 '16 at 22:37
  • The output is :- `int 953738240` – RobertPuerco Aug 18 '16 at 22:39
  • Okay, what has happened is your PHP_INT_MAX is lower than other servers. The int value being casted to cannot be represented as an int on your system, it is wrapping around to the negative PHP_INT_MAX when the biggest number possible is reached, then carrying on past 0 to the number you quoted. This is called an integer overflow and is discussed here: https://secure.php.net/manual/en/language.types.integer.php – jedifans Aug 18 '16 at 22:44
  • How can I increase that value? – RobertPuerco Aug 18 '16 at 22:44
  • Your system cannot, you can work around it by storing as a float or by reading through https://stackoverflow.com/questions/37391/arithmetic-with-arbitrarily-large-integers-in-php – jedifans Aug 18 '16 at 22:47
  • Once you implement one of those solutions, please ensure you either cancel your previous downvote of my answer or accept one of the two answers on this question. Both solutions are valid on 64 bit systems, which is the usual platform since at least 2009. – jedifans Aug 18 '16 at 22:50