echo'ing large integers produces scientific notation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mu-ziq
Forum Newbie
Posts: 11
Joined: Fri Jul 08, 2005 9:42 pm

echo'ing large integers produces scientific notation

Post by mu-ziq »

I want to output large integers that I get from database to screen but they always come out in scientific notation.

Code: Select all

$n = 10000000000000000000000000000;
echo 'number: '.$n; // outputs "number: 1E+028"

I know that's how php represents long numbers but I want my output to say 10000000000000000000000000000 not 1E+028.

I'm wondering if there is a built in function or a way that will automatically convert (scientific float) -> (normal string) or if I have to write one myself.

Thanks a lot.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP does not process large integers natively. They are automatically converted to floating point after you reach a certain threshold. What that is, depends on the build you run it on. Use GMP or BC instead.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: echo'ing large integers produces scientific notation

Post by daedalus__ »

mu-ziq wrote: I'm wondering if there is a built in function or a way that will automatically convert (scientific float) -> (normal string) or if I have to write one myself.
$n = '10000000000000000000000000000';

If the database stores them as integers maybe you could use mysql to cast them so they come back as strings or something?

I don't think it has to be as complicated as a function but I've never had to deal with this sort of thing so I could be wrong.
Post Reply