java 对于货币/金钱,我应该使用哪种 XML 数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6454256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:56:58  来源:igfitidea点击:

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

javaxmlcurrencydata-storage

提问by Zac Blazic

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 formatwhen storing the data, and "parsed" back again when I read the data.

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

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

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

采纳答案by Cris

i used this

我用过这个

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

回答by Michael Kay

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 的一个很好的测试是:人类读者能否在不查阅文档的情况下正确猜出这意味着什么?并非每个人都认同这种需求观点,而且有时这是不切实际的。但是省略小数点只是要求数据在沿线某处被错误处理。

回答by Vihung

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 货币代码,以避免在用户眼中出现任何混淆 - 例如,他们可能不知道英镑是 GBP 还是 UKP。