我应该对货币/货币使用哪种 XML 数据类型?
在阅读了一些关于货币/货币使用什么 Java 数据类型的问题后,我决定使用 int/long 方法,在该方法中将货币/货币的值存储为其最低单位(例如美分)。
这也适用于存储数据(以 XML 形式)吗?
我首先想到的是不,因为它只会被“解析”为 XML 十进制格式存储数据时,并在读取数据时再次“解析”回来。
欢迎任何建议,因为我对此很陌生。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难为您提供一个对需求输入如此之少的设计,但我绝不会在没有货币的情况下将金额放入 XML 中:
但是如果您在美国,如果您敢于建议他们处理,人们可能会像对待来自外太空的人一样对待您多种货币,所以您的情况可能会有所不同...
我认为,检验 XML 设计是否良好的一个很好的测试是:人类读者是否会在不查阅文档的情况下正确猜测这意味着什么?并不是每个人都同意这种需求观点,而且有时这是不切实际的。但省略小数点只是要求数据在沿线的某个地方被错误处理。
Hard to give you a design with so little input on requirements, but I would never put money amounts in XML without the currency:
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.
我用过这个
i used this
我已经使用了这个,
这让我可以将我的货币金额表示为三字符 (ISO 4217) 货币代码和限制为小数点后两位的金额的组合。
您也许可以更进一步,将货币代码定义为枚举类型,指定所有有效的 ISO 4217 货币代码,以避免用户眼中的任何混淆 - 例如,他们可能不知道英镑是 GBP 还是 UKP。
I have used this
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.