我应该使用哪种XML数据类型用于货币/货币?

时间:2022-02-17 16:31:50

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).

在阅读了几个关于用于货币/货币的Java数据类型的问题之后,我决定使用int / long方法,其中将货币/货币的值存储为最低单位(例如美分)。

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

这是否也适用于存储数据(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.

我想到的第一件事是,因为它在存储数据时只会被“解析”为XML十进制格式,而在读取数据时会再次“解析”回来。

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

欢迎任何建议,因为我是新手。

3 个解决方案

#1


6  

i used this

我用过这个

<xsd:simpleType name="money">
      <xsd:restriction base="xsd:decimal">
         <xsd:fractionDigits value="2"/>
      </xsd:restriction>
</xsd:simpleType>

#2


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:

很难给你一个设计,对需求的贡献如此之少,但如果没有货币,我绝不会把钱存入XML:

<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.

我认为对你是否设计好XML的一个很好的考验是:人类读者是否会正确猜出这意味着什么,而不是为了获得文档?不是每个人都同意这种要求的观点,这有时是不切实际的。但是省略小数点只是要求在沿线的某处错误处理数据。

#3


1  

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.

这使我可以将我的货币金额表示为三字符(ISO 4217)货币代码和限制为两位小数的金额的组合。

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.

您可以更进一步,将货币代码定义为枚举类型,指定所有有效的ISO 4217货币代码,以避免用户眼中的任何混淆 - 例如,他们可能不知道英镑是英镑还是英镑。

#1


6  

i used this

我用过这个

<xsd:simpleType name="money">
      <xsd:restriction base="xsd:decimal">
         <xsd:fractionDigits value="2"/>
      </xsd:restriction>
</xsd:simpleType>

#2


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:

很难给你一个设计,对需求的贡献如此之少,但如果没有货币,我绝不会把钱存入XML:

<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.

我认为对你是否设计好XML的一个很好的考验是:人类读者是否会正确猜出这意味着什么,而不是为了获得文档?不是每个人都同意这种要求的观点,这有时是不切实际的。但是省略小数点只是要求在沿线的某处错误处理数据。

#3


1  

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.

这使我可以将我的货币金额表示为三字符(ISO 4217)货币代码和限制为两位小数的金额的组合。

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.

您可以更进一步,将货币代码定义为枚举类型,指定所有有效的ISO 4217货币代码,以避免用户眼中的任何混淆 - 例如,他们可能不知道英镑是英镑还是英镑。