Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 751
Zac Blazic Avatar asked Jun 23 '11 12:06

Zac Blazic


1 Answers

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.

like image 132
Vihung Avatar answered Oct 13 '22 20:10

Vihung