Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric converted to float instead of string #66

Closed
martinoshub opened this issue Oct 9, 2024 · 3 comments
Closed

Numeric converted to float instead of string #66

martinoshub opened this issue Oct 9, 2024 · 3 comments

Comments

@martinoshub
Copy link

martinoshub commented Oct 9, 2024

Hi guys. Please tell me that there's a way without altering your code to make sure that when fetching a row from the database, a Numeric type gets converted to string instead of float? Choosing to make float the default type means there's potential data loss and no way to recover. Whereas with a string type I could always convert it to float if I wanted to.

Internal/PgSqlResultIterator.php

        'N' => match ($oid) { // Numeric
            700, 701, 1700 => (float) $value, // float4, float8, and numeric to float
            790 => $value, // money includes currency symbol as string
            default => (int) $value, // All other numeric types cast to an integer
        },

It would be great to have a way to change this behavior while setting up the class so that numerics are returned as a string. Thanks so much. And thank you for this wonderful library by the way, I really love Amphp with fibers and it's great that there's a Postgres lib for it.

@Bilge
Copy link
Contributor

Bilge commented Oct 9, 2024

If you want to store floats as stings then change the column type.

@martinoshub
Copy link
Author

If you want to store floats as stings then change the column type.

I don't want to store floats. I don't want to use floats. I want to work with precise numeric types. That is Numeric() in Postgres. The closest thing in PHP (assuming data is preserved, which is a must when you're managing financial entries) is a string.

@trowski
Copy link
Member

trowski commented Oct 11, 2024

Hi @martinoshub!

Thank you for pointing out this issue. This was a discrepancy between the ext-pq implementation, which returns a string) and the ext-pgsql implementation, which was wrongly returning a float. I've updated the ext-pgsql implementation to also return a string.

The fix has been tagged under v2.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants