Skip to content

Commit

Permalink
Ensure amounts are marshaled with two digits after the decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
printesoi committed Apr 2, 2024
1 parent 2be8105 commit 98bb273
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions xml_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ type AmountWithCurrency struct {
CurrencyID CurrencyCodeType `xml:"currencyID,attr,omitempty"`
}

// MarshalXML implements the xml.Marshaler interface. We use a custom
// marshaling function for AmountWithCurrency to ensure two digits after the
// decimal point.
func (a AmountWithCurrency) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
type amountWithCurrency struct {
Amount string `xml:",chardata"`
CurrencyID CurrencyCodeType `xml:"currencyID,attr,omitempty"`
}
xmlAmount := amountWithCurrency{
Amount: a.Amount.StringFixed(2),
CurrencyID: a.CurrencyID,
}
return e.EncodeElement(xmlAmount, start)
}

// ValueWithAttrs represents and embeddable type that stores a string as
// chardata and a list of attributes. The name of the XML node must be
// controlled by the parent type.
Expand Down

0 comments on commit 98bb273

Please sign in to comment.