decimal icon indicating copy to clipboard operation
decimal copied to clipboard

How to convert a string to NullDecimal?

Open mrwh1t4 opened this issue 3 years ago • 2 comments

How to convert a string to NullDecimal? I use NullDecimal in gorm correspond to decimal(50,15) NOT NULL in mysql

mrwh1t4 avatar Aug 04 '21 07:08 mrwh1t4

Take a look in documentation, it's right in Overview section: https://pkg.go.dev/github.com/shopspring/decimal?utm_source=godoc#pkg-overview

The best way to create a new Decimal is to use decimal.NewFromString, ex:

n, err := decimal.NewFromString("-123.4567")
n.String() // output: "-123.4567"

Create NullDecimal from Decimal dec:

nullD := decimal.NullDecimal{Valid: true, Decimal: dec}

edbond avatar Aug 04 '21 08:08 edbond

Thanks for explaining this @edbond <3

mwoss avatar Aug 04 '21 21:08 mwoss