Jump to content

Archived

This topic is now archived and is closed to further replies.

colabus

PHP Number Handling - Scientific Notation

Recommended Posts

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

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

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

Share this post


Link to post
Share on other sites
haha it would seem I was doing too much to get it working.

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

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

Cheers :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.