Coder Social home page Coder Social logo

Comments (6)

jackc avatar jackc commented on June 13, 2024 2

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.

jackc avatar jackc commented on June 13, 2024 1

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.

jackc avatar jackc commented on June 13, 2024 1

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.

nkev avatar nkev commented on June 13, 2024

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.

nkev avatar nkev commented on June 13, 2024

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.

jayd3e avatar jayd3e commented on June 13, 2024

Thanks for adding this! It's going to majorly help me out on this latest project(financial-related).

from pgx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.