.NET 的货币数据类型?

发布于 2024-08-20 08:16:28 字数 1001 浏览 5 评论 0 原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

财迷小姐 2024-08-27 08:16:28

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

与他有关 2024-08-27 08:16:28

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.

我很坚强 2024-08-27 08:16:28

代码项目的货币数据类型

http://www.codeproject.com/moneydatatype.aspx codeproject.com/KB/vb/moneyDatatype.aspx

作者指出类似的问题:

作为我最近申请的一部分
意识到 .NET 是多么缺乏
货币支持,别误会我的意思,
有很多“碎片”但是胶水
因为所有项目都丢失了
,所以这个
文章是对此的回应。

并实现目标

我的主要目标变成了

  • 存储货币类型和值(例如澳元、美元、丹麦克朗)。
  • 存储格式详细信息(即小数分组、货币符号、
    等)。
  • 转换提供商,我不想对其进行硬编码,因为它是一种数据类型
    而不是解决方案。
  • 开发安全功能(即防止在不同的
    货币类型)。

到目前为止,这是最接近我正在搜索的 .NET 代码。它满足了金钱的大部分要求。

如果有人有更好的东西,将不胜感激。

Money Data Type @ The Code Project

http://www.codeproject.com/KB/vb/moneyDatatype.aspx

Author states similar problem:

as part of a recent application I
realized how lacking .NET is for
currency support, don't get me wrong,
there are many "pieces" but the glue
for all items is missing
, so this
article is a response to that.

and fulfills objectives

my main objectives became

  • Store the currency type with the value (i.e. AUD, US, DKK).
  • Store formatting details (i.e. decimal grouping, currency symbols,
    etc.).
  • Conversion providers, I didn't want to hard code this as it is a datatype
    and not a solution.
  • Development safety features (i.e. prevent arithmetic on different
    currency types).

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.

娇纵 2024-08-27 08:16:28

如果您正在寻找模式,可以查看 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.

浮光之海 2024-08-27 08:16:28

看看这里:

http ://blogs.msdn.com/lucabol/archive/2008/12/04/financial-functions-for-net-released.aspx

它提供了一个复制所有 Excel 财务函数的 .NET 库。

进行货币转换很棘手,因为显然它会不断变化,因此硬编码的值或多或少是无用的。但是,您可以使用网络服务来获取最新的汇率。 看起来是一个好的开始。更好的是,同一个转换器的 REST 风格的接口:

http://www.webservicex.com/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=EUR

这样就可以输出英镑到欧元的汇率。

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:

http://www.webservicex.com/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=EUR

So that outputs the conversion rate of pounds sterling to euros.

云仙小弟 2024-08-27 08:16:28

您可能会发现创建自己的类将产生最佳解决方案。

you will probably find that creating your own class will result in the best solution.

岁月蹉跎了容颜 2024-08-27 08:16:28

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

傲影 2024-08-27 08:16:28

我确实理解您关于拥有抽象层而不是金钱的好处的观点。然而,我对金钱及其“运作”的看法相当模糊。对于诸如 File 之类的其他内容,我很清楚应该有 OpenReadWrite关闭操作。但对于 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 be Open, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文