Which XML data type should I use for currency/money?
Asked Answered
C

3

11

After reading a few questions on what Java data type to use for currency/money, I have decided to use the int/long method, where you store the value of the currency/money as its lowest unit (such as cents).

Does this apply to storing the data as well (in XML)?

The first thing that comes to my mind is no, because it will just be "parsed" into an XML decimal format when storing the data, and "parsed" back again when I read the data.

Any suggestions are welcome, as I am new to this.

Cazares answered 23/6, 2011 at 12:49 Comment(0)
C
7

i used this

<xsd:simpleType name="money">
      <xsd:restriction base="xsd:decimal">
         <xsd:fractionDigits value="2"/>
      </xsd:restriction>
</xsd:simpleType>
Carrigan answered 23/6, 2011 at 12:51 Comment(3)
a complete sample here : java2s.com/Code/XML/XML-Schema/fractionDigitsformoneytype.htmCarrigan
I assume you mean fractionDigits as a restriction for decimal, not scalePedro
Depending on the currency you will need 0 to 3 fraction digits. So the restriction is invalid.Verne
B
9

Hard to give you a design with so little input on requirements, but I would never put money amounts in XML without the currency:

<amount currency="GBP">230.45</amount>

But if you're in the US people may treat you like someone from outer space if you dare to suggest they handle multiple currencies, so your mileage may vary...

I think a good test of whether you've designed XML well is: will a human reader guess correctly what this means without reaching for the documentation? Not everyone shares that view of the requirements, and it's sometimes impractical. But omitting the decimal point is just asking for the data to be misprocessed somewhere along the line.

Butyl answered 23/6, 2011 at 15:15 Comment(1)
I agree with the the readability point. Both the answers I got here today meld very well together, thanks guys.Cazares
C
7

i used this

<xsd:simpleType name="money">
      <xsd:restriction base="xsd:decimal">
         <xsd:fractionDigits value="2"/>
      </xsd:restriction>
</xsd:simpleType>
Carrigan answered 23/6, 2011 at 12:51 Comment(3)
a complete sample here : java2s.com/Code/XML/XML-Schema/fractionDigitsformoneytype.htmCarrigan
I assume you mean fractionDigits as a restriction for decimal, not scalePedro
Depending on the currency you will need 0 to 3 fraction digits. So the restriction is invalid.Verne
P
2

I have used this

<complexType name="PaymentAmountType">
  <annotation><documentation>Type representing a payment amount (e.g. price or total)</documentation></annotation>
  <attribute name="currencyCode" type="payment:CurrencyCodeType" use="required"/>
  <attribute name="amount" type="payment:AmountType" use="required"/>
</complexType>

<simpleType name="AmountType">
  <restriction base="decimal">
    <fractionDigits value="2"/>
  </restriction>
</simpleType>

<simpleType name="CurrencyCodeType">
  <restriction base="string">
    <minLength value="3"/>
    <maxLength value="3"/>
    <pattern value="[A-Z]{3}"/>
  </restriction>
</simpleType>

This lets me represent my monetary amounts as a combination of a three-character (ISO 4217) currency code and an amount restricted to two decimal places.

You could perhaps go further and define the currency code as an enumerated type that specifies all the valid ISO 4217 currency codes to avoid any confusion in the eyes of the user - for example, they may not know whether British Pound Sterling is GBP or UKP.

Pedro answered 15/5, 2013 at 21:4 Comment(1)
Annotations are handy things when defining enumerated types.Stogy

© 2022 - 2024 — McMap. All rights reserved.