Jump to content


Photo

PHP Number Handling - Scientific Notation


  • Please log in to reply
1 reply to this topic

#1 colabus

colabus
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 June 2006 - 10:44 AM

Hi all, have come across a small issue with some code I'm writing. Basically my users will enter in a serial number (alphanumeric) and then specify a quantity to add. My code then takes this and increments from the original.

I've had to consider a number of possible issues.

Using the ++ operator the serial will increase, however if the number is quite large it then if converted to scientific notation.

To give you an idea I type in: 04000000000000000001 (quantity of 3), so I'd expect to get:

04000000000000000002
04000000000000000003

However I'm getting:

0000000000000004E+18
0000000000000004E+18

            if ($increment == 1)
            {
                $serialCutEnd = preg_replace('/[0-9]*$/', '', $serial);
                $serialEnding = substr($serial, strlen($serialCutEnd));

This handles quite well but when the number gets big I run into trouble. Is there a way to force the value to remain a string? I've tried type casting but it seems as soon as I use the ++ operator issues arise.

I'd appreciate any guidance on this.

Cheers

#2 colabus

colabus
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 June 2006 - 10:54 AM

haha it would seem I was doing too much to get it working.

function increment($var) {
   $var2 = '_'.$var;
   return substr(++$var2,1);
}

php.net/manual/en/language.operators.increment.php#62479

Cheers :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users