Jump to content

PHP Number Handling - Scientific Notation


colabus

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
Link to comment
https://forums.phpfreaks.com/topic/12928-php-number-handling-scientific-notation/
Share on other sites

Archived

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

×
×
  • Create New...

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.