为 .NET 寻找一个良好的货币数据类型,支持货币和汇率(具有相关的行为和操作)。
- 注意:我开始搜索书中印刷的源代码 测试驱动开发示例,作者 Kent Beck - 他提出了一个很好的货币概念。网上找不到完整的源码。这本书不包含一个单一的列表 - 相反,它在本书的整个过程中开发了源代码。
虽然这本书没有更深入,但我也希望 Money 类支持不同的舍入机制< /strong> 因为全球金融机构之间的情况也各不相同。
编辑 1: 澄清
我所说的“货币类”是指可用于支持货币及其操作的 .NET 数据类型,包括货币支持和汇率计算。类似的东西是更高级别的抽象,并且可能在内部使用原始数据类型。它还可能依赖于一系列类,如:Money、ExchangeRate、Bank 等。
它可能是值类型(即结构)或引用类型(即类),但无论哪种方式它都是完整的并支持上述功能。
编辑 2:Money 数据类型的目标
(这也说明了为什么原始小数、int 或其他基本类型不能满足所有 Money 需求)
Money 数据类型的目标是:
- 安全功能(即防止对不同货币类型进行算术 ) )。
- 存储带有值的货币类型(即 AUD、US、DKK)。
- 存储格式详细信息(即十进制分组、货币符号等)。
- 转换提供商(例如汇率)可帮助完善解决方案。
- 减少表达式中的多种货币以实现结果。
注意:可以从外部源加载并使用不同的数据值(例如汇率)。这个问题并不意味着任何动态都被硬编码到金钱的概念中。
Looking for a good Money data type for .NET that supports currencies and exchange rates (with related behaviour & operations).
- Note: I started searching for the source code seen in print in the book Test-Driven Development By Example by author Kent Beck - he develops a nice Monetary concept. Unable to find the complete source online. The book does not contain one singular listing - instead it develops the source code over the duration of the book.
Although the book doesn't go deeper I would also like the Money class to support different rounding mechanisms because that also varies among financial institutions around the globe.
Edit 1: Clarifications
By "Money class" I mean a .NET data type that I can use to support money and its operations, complete with currency support and exchange rate calculations. Something like that is a higher level abstraction and might internally use a primitive data type. Also it might rely on a bundle of classes like: Money, ExchangeRate, Bank, etc.
It might be a value type (i.e. struct) or reference type (i.e. class) but either way it would be complete and support the aforementioned features.
Edit 2: Objectives of Money data type
(This also shows why a raw decimal, int or other primitive type won't suffice for all Money needs)
Objectives of a Money data type would be:
- Safety features (i.e. prevent arithmetic on different currency types).
- Store the currency type with the value (i.e. AUD, US, DKK).
- Store formatting details (i.e. decimal grouping, currency symbols, etc.).
- Conversion providers (e.g. exchange rate) to help round out the solution.
- Reduce multiple currencies in an expression to achieve a result.
Note: Varying data values like exchange rates can be loaded from an external source and used. This question does not infer anything dynamic is hard-coded into the concept of Money.
发布评论
评论(8)
Martin Fowler 将货币视为“数量”的特例,其次他认为货币的正确数据类型应该是大整数。他确实有道理。
Martin Fowler 的数量和货币模式
Martin Fowler considers money as a special case of "Quantity", secondly he thinks the right Data Type for money should be the Big Integer. And he does have a point.
Quantity and Money Pattern by Martin Fowler
NodaMoney 提供了一个库,它将 Money 视为 .NET 中的一等公民,并处理所有像货币这样丑陋的部分和格式化。
它符合 ISO 4217 中的货币。它是 java 库 JodaMoney 的 .NET 对应版本。
NodaMoney provides a library that treats Money as a first class citizen in .NET and handles all the ugly bits like currencies and formatting.
It complies with the currencies in ISO 4217. And it's the .NET counterpart of the java library JodaMoney.
代码项目的货币数据类型
http://www.codeproject.com/moneydatatype.aspx codeproject.com/KB/vb/moneyDatatype.aspx
作者指出类似的问题:
并实现目标
到目前为止,这是最接近我正在搜索的 .NET 代码。它满足了金钱的大部分要求。
如果有人有更好的东西,将不胜感激。
Money Data Type @ The Code Project
http://www.codeproject.com/KB/vb/moneyDatatype.aspx
Author states similar problem:
and fulfills objectives
So far this the closest .NET code to what I'm searching for. It fulfills most requirements of Money.
If anybody has something better it would be much appreciated.
如果您正在寻找模式,可以查看 Joda Money。它是 Java,但应该会给您一些关于 API 的想法。由于运算符重载,AC# 实现将更加简洁。
If you are looking for patterns, you could check out Joda Money. It is Java, but should give you some ideas on an API. A C# implementation would be much less verbose due to operator overloading.
看看这里:
http ://blogs.msdn.com/lucabol/archive/2008/12/04/financial-functions-for-net-released.aspx
它提供了一个复制所有 Excel 财务函数的 .NET 库。
进行货币转换很棘手,因为显然它会不断变化,因此硬编码的值或多或少是无用的。但是,您可以使用网络服务来获取最新的汇率。 这看起来是一个好的开始。更好的是,同一个转换器的 REST 风格的接口:
这样就可以输出英镑到欧元的汇率。
Have a look here:
http://blogs.msdn.com/lucabol/archive/2008/12/04/financial-functions-for-net-released.aspx
It provides a .NET library replicating all the excel financial functions.
Doing currency conversion is tricky, because obviously it changes continously, so hardcoded values will be more or less useless. However, you may be able to use a web service to access up-to-date exchange rates. This one looks like a good start. Even better, a REST-style interface to the same converter:
So that outputs the conversion rate of pounds sterling to euros.
您可能会发现创建自己的类将产生最佳解决方案。
you will probably find that creating your own class will result in the best solution.
我是 NMoneys 的作者,我认为它可能会很方便。
它还没有“正式发布”,但在正式发布之前它不会有什么变化。
I am the author of NMoneys which I think it might come handly.
It has not been "officially released" yet, but it will change very little until it does.
我确实理解您关于拥有抽象层而不是金钱的好处的观点。然而,我对金钱及其“运作”的看法相当模糊。对于诸如
File
之类的其他内容,我很清楚应该有Open
、Read
、Write
、关闭
操作。但对于 Money,除了基本的数学运算(+、-、*、/)之外,我想不出太多其他的东西。我知道的 C++ 定量金融库之一确实包含这个 Money 抽象(http://quantlib.org/reference/class_quant_lib_1_1_money.html。)但是你可以清楚地看到这是一个非常薄的包装器,它提供了基本的运算符Decimal 上的重载和单位转换。
在大多数情况下,我认为十进制可以满足您的要求。如果您需要支持特定的货币操作,我认为推出您自己的课程是可以的。
I do understand your points about the benefit of having abstraction layer over money here. However, my view on money and its "operations" is quite blurry. For other things like
File
, it's clear to me there should beOpen
,Read
,Write
,Close
operations. But for Money, I can't think of much other than basic math operations (+,-,*,/)One of C++ quantitative finance lib I know does contain this Money abstraction (http://quantlib.org/reference/class_quant_lib_1_1_money.html.) But you can clearly see that this is a very thin wrapper which provide basic operator overloads and unit conversion over Decimal.
In most cases, I think decimal can fulfill your requirement. If there is specific Money operation you need to support, I think it's okay to roll out your own classes.