Comments (6)
For what it's worth, in 9f9a977 I added compatibility with the standard database/sql.Scanner and database/sql/driver.Valuer interfaces for custom types. This means you can use https://github.com/shopspring/decimal as a type that maps to PostgreSQL numeric. See https://github.com/jackc/pgx/blob/master/query_test.go#L913 for an example.
from pgx.
Unfortunately, there is no decimal type in the Go standard library so numeric columns will be returned as text. It is relatively simple to create custom type mappings (https://github.com/jackc/pgx/blob/master/example_custom_type_test.go) once you've chosen a type to represent it in Go.
For the JSONB type it should be returned as a string by default.
from pgx.
Well, it's not a problem specific to Go, but you probably don't want to use floats to represent money.
http://spin.atomicobject.com/2014/08/14/currency-rounding-errors/
It's unfortunate that Go doesn't have a decimal type in its standard library. I haven't had to deal with money in my Go applications yet, so I don't have a good suggestion for a 3rd party decimal library, but there may be one. Another option is to convert to integers and there money as the number of cents.
from pgx.
Thanks. I think that custom type mapping code is a bit out my reach as a newbie Gopher :)
I just did a conversion to float64 and it worked fine (I got my $27.95 Postgres decimal value):
product.Price, err = strconv.ParseFloat(sPrice, 64)
Are there any issues with this? Is this the common practice for working with Postgres datatypes not supported in Go?
from pgx.
Thanks for the tip. I may as well use an int64 in both Postgres and the Go model to store and calculate the product price in cents and only add the dot when displaying the price in the browser. There may even be a small performance advantage for using integers...
from pgx.
Thanks for adding this! It's going to majorly help me out on this latest project(financial-related).
from pgx.
Related Issues (20)
- Allow registering a callback after current transaction commits HOT 2
- Support scanning time type into string HOT 2
- Param overriding doesn't work for some fields HOT 1
- Allow defining `tls.Config.CipherSuites` HOT 1
- Custom JSON marshal/unmarshal HOT 3
- change of tenant for multi tenancy using pgxpool is not working HOT 8
- Should pgxpool be closed after each use during the life time of the program? HOT 2
- Running Query Is Not Cancelled on Context Cancelled HOT 4
- Request for guidance on tracing and logging HOT 5
- Prepare doesn't support named arguments HOT 3
- Why the row affected still have no zero even field in column are not updated actually same value
- Accessing replies in pipeline mode without a sync HOT 3
- Cannot scan into custom composite type (CockroachDB) HOT 1
- Provide improved support for loading types HOT 4
- allow configuring the Config directly, without having to call `ParseConfig` HOT 3
- time.Time and timestamp HOT 5
- panic on scanning to slice of custom types HOT 2
- Interval of 0s is invalid HOT 4
- Add LoadTypes method for bulk type registration HOT 2
- Scan not only anonymous structs HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgx.