3

Need something like intVal() but for bigger numbers, for example: 100001416147426.

Is there a function like this in php? Can't find it.

degressor
  • 358
  • 4
  • 16

3 Answers3

2

You should use BC Math, it is designed to numbers of any size with PHP.

The BC Math extensions offers several mathematic functions like:

  • bcadd Add two arbitrary precision numbers
  • bccomp — Compare two arbitrary precision numbers
  • bcsqrt Get the square root of an arbitrary precision number
  • ...

On the PHP documentation site there is a small code example by Charles to round a long number!

powtac
  • 40,542
  • 28
  • 115
  • 170
1

coding standard in the past (since C) has been to follow the number with an L/l $x = 100001416147426L;

This cue's the parser to allocate 64 bits in order to read the number out of the script to compile.

But unless you are running the php x64 this will be useless. Other wise you will have to build it out of a big_number component. Once in a x64 environment intval will automatically expand to a long when exceeding a 32-bit int.

ppostma1
  • 3,616
  • 1
  • 27
  • 28
  • This will break when read/writing to certain database engines before php 5.3. The php database drivers were hard coded with a 32-bit signed definition – ppostma1 Mar 06 '17 at 22:05
1

consider

$x = (double) "100001416147426";
var_dump($x);

output:

float(1.0000141614743E+14)
jancha
  • 4,916
  • 1
  • 24
  • 39
  • -1 Notice how in that very example, the precision of the value is lost. – Lightness Races in Orbit Sep 05 '11 at 15:58
  • precision is not lost :P do some math around first. – jancha Sep 05 '11 at 16:14
  • OK, actually that *will* fit in a `double` **on some systems**. Demonstrating with `printf` [shows this](http://codepad.org/X8Kyz6vM), whereas `var_dump` does not (your example demonstrates rounding for output). This is still [not an appropriate thing to rely on for "big" values](http://codepad.org/XTZg3L9q), though. – Lightness Races in Orbit Sep 05 '11 at 16:56
  • And what does "math around" mean? – Lightness Races in Orbit Sep 05 '11 at 16:57
  • try this: – jancha Sep 06 '11 at 09:20
  • also, you can try even this: $int = (double) 123456789; $long = $int * $int * $int * $int; var_dump($long); $int = sqrt(sqrt($long)); var_dump($int); still no precision loss. therefore, please review your response. – jancha Sep 06 '11 at 09:25
  • but why -1 :) I mean I did not suggest anything wrong. I mean what are the rules for down vote then. not happy with this. – jancha Sep 06 '11 at 20:53
  • Although you have correctly pointed out that the specific given value will fit in a double: (a) the OP asked for an equivalent for `intVal`, which deals with integers; (b) this is not the canonical, generic, scalable or correct way to deal with "bigger numbers" -- the implication, I think, is clearly that "bigger numbers" means "numbers bigger than that which we can guarantee will fit in PHP's `int` type (powtac's answer is). I'm sorry that you disagree, but voting is voting! And it's just one vote; I wouldn't worry too much about it. :) – Lightness Races in Orbit Sep 06 '11 at 20:57
  • my first down vote.. just wanted that to be justified. as you can see that I'm not really wrong, it's just different perspective. waiting for up vote from you :) – jancha Sep 08 '11 at 13:35
  • Don't beg for upvotes or tell people to upvote you. I explained why the downvote. – Lightness Races in Orbit Sep 08 '11 at 13:40
  • I'm not begging. we need court at stack overflow to settle disputes like this. – jancha Sep 08 '11 at 13:42
  • No we don't. We have a democracy through the right to vote. I downvote; at the same time, other people are free to upvote. – Lightness Races in Orbit Sep 08 '11 at 14:28
  • and indeed so they do. – jancha Oct 08 '13 at 13:51
  • ... which is a shame, because this answer is still wrong more than two years later. – Lightness Races in Orbit Oct 08 '13 at 15:28
  • I agree that (double) is really limited to 64bit - but if your "big" number fits within, it's way to go. So really, it's the application which should determine the route to go. – jancha Oct 09 '13 at 07:02