Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hotsaucejake's avatar

PHP limitations with converting numbers to string - strval() - and precision

Problem: storing the input of any given number (whether it's an integer or decimal) in mysql and returning the same exact number to the user when called.

Desired solution: Take a numeric input and store it in a mysql table - I don't care how the table is formatted - with the ability to return the exact same number as it was originally input.

I was playing around with converting the number to a string, checking whether or not it was positive, and storing it along 3 columns of value power_of_ten and positive_value (boolean). This seemed to work great until the numbers started to become large. Now I KNOW there are limitations in the sizes of what can be stored in mysql but I was hoping to get closer to those limitations than I currently am. I'd also be ok with converting the number to a string and storing the left side of the decimal (if any) to a column, the right side of the decimal (if any) in another column and whether or not it is positive.

For example, I know the max unsigned integer value is 18446744073709551615 - I was hoping to be able to get close in approaching this limitation and throw an error if a number is outside of this bounds. But currently, I can't even get close to that from converting a decimal to a string.

I'm trying to convert large (up to 20 digits) numbers into strings. However, once you surpass 14 or 15 digits the results of converting a number to a string start to become rounded.

I was playing around with the 'precision' value in my php.ini settings - the default is 14 - however the results became even more skewed.

Here's an example of the limitation I'm facing:

strval(1844674407.3709551615) outputs "1844674407.371"

I can get even closer with: rtrim(rtrim(sprintf('%.8F', 1844674407.3709551615), '0'), "."); outputs "1844674407.37095523"

I really want an unsigned integer to be my limitation rather than converting a decimal to a string be my limitation.

0 likes
2 replies
ganeshghalame's avatar

Better if you use decimal rather than string,

$table->decimal('amount', 20, 8);

This will create a decimal column in the database with a precision of 20 digits, including 8 digits after the decimal point

hotsaucejake's avatar

@ganeshghalame the input could be any given int or decimal, I want to store for each. Specifying a decimal column wouldn't work in this case because I don't know the length of the decimal itself.

Please or to participate in this conversation.