mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Added support for Decimal places in JSON marshall
This commit is contained in:
parent
cd690d0c9e
commit
cbaf07db27
1 changed files with 161 additions and 150 deletions
15
decimal.go
15
decimal.go
|
@ -52,6 +52,10 @@ var DivisionPrecision = 16
|
|||
// silently lose precision.
|
||||
var MarshalJSONWithoutQuotes = false
|
||||
|
||||
// If Set to True, returns rounded fixed-point string with places digits after the decimal point
|
||||
var MarshalJSONWithDecimalPlaces = false
|
||||
var MarshalJSONDecimalPlaces = 0
|
||||
|
||||
// Zero constant, to make computations faster.
|
||||
var Zero = New(0, 1)
|
||||
|
||||
|
@ -931,11 +935,18 @@ func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error {
|
|||
|
||||
// MarshalJSON implements the json.Marshaler interface.
|
||||
func (d Decimal) MarshalJSON() ([]byte, error) {
|
||||
var stringVal string
|
||||
if MarshalJSONWithDecimalPlaces {
|
||||
stringVal = d.StringFixed(MarshalJSONDecimalPlaces)
|
||||
} else {
|
||||
stringVal = d.String()
|
||||
}
|
||||
|
||||
var str string
|
||||
if MarshalJSONWithoutQuotes {
|
||||
str = d.String()
|
||||
str = stringVal
|
||||
} else {
|
||||
str = "\"" + d.String() + "\""
|
||||
str = "\"" + stringVal + "\""
|
||||
}
|
||||
return []byte(str), nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue