简体   繁体   English

如何使用CultureInfo格式化已弃用的货币?

[英]How to use CultureInfo to format deprecated currencies?

In dotnet, the recommended way of formatting currencies in a culture-specific way, is (as far as I can find): 在dotnet中,以文化特定方式格式化货币的推荐方法是(据我所知):

using System.Globalization
var info = CultureInfo.GetCultureInfo("en-GB")
return string.Format(info, "{0:c}", 1234.5678)

This returns: 返回:

 £1,234.57

However. 然而。 No specific currency is given here. 此处未给出具体货币。 So if Great Britain ever converts to Euro, the same method would suddenly return something like: 因此,如果英国转换为欧元,同样的方法会突然返回如下:

 € 1,234.57

This has just happened to Latvia. 这恰好发生在拉脱维亚。 This year, it converted to the euro. 今年,它转换为欧元。 Various systems contain both amounts in letvian lats and euros. 各种系统包含letvia lats和euros两种金额。 I need to be able to still print and format old currencies. 我需要能够打印和格式化旧货币。 And I need to be sure new currencies can also be handled. 我需要确保新货币也可以处理。 This formatting is not just about the symbol, but also about the placement (before, after, with or without space) and the local number format (decimal separator and thousands separator). 此格式不仅与符号有关,还与放置(前后,有或没有空格)和本地数字格式(小数分隔符和千位分隔符)有关。

Every amount in our database has a currency (eg EUR) and a corresponding culture (eg nl-NL). 我们数据库中的每个金额都有货币(例如EUR)和相应的文化(例如nl-NL)。 Euro amounts are formatting differently depending on if they originate from our German or our Dutch office. 欧元金额的格式不同,具体取决于它们来自我们的德国或荷兰办事处。 (they both use euros, but format the amounts differently) (他们都使用欧元,但格式不同)

  • Does dotnet provide access to older currencies? dotnet是否提供对旧货币的访问?
  • What is a future-proof way of writing the formatting amounts? 什么是编写格式量的未来证明方式?

Right now, the only way I can think of, is to copy the culture-database to my database. 现在,我能想到的唯一方法是将culture-database复制到我的数据库。

You can create a custom CultureInfo (by cloning one and modifying) which uses a different currency symbol/format (ie. set its NumberFormat to a different instance of NumberFormatInfo ). 可以创建一个定制CultureInfo (由克隆一个和修改),其使用不同的货币符号/格式(即它的设置NumberFormat到的不同实例NumberFormatInfo )。

Then pass this CultureInfo to formatting functions as needed. 然后根据需要将此CultureInfo传递给格式化函数。

As the comment to the question notes, .NET (and Windows in general) doesn't provide historic data (similarly for time zones but there's a library for that ). 正如对问题的评论所述,.NET(以及一般的Windows)不提供历史数据(类似于时区,但有一个 )。 In the cases you need you'll need to hold enough data yourself. 在您需要的情况下,您需要自己保存足够的数据。

Remember ISO-4217 currency codes are not reused under such a change, so holding that against amounts allows you to format correctly. 请记住, ISO-4217货币代码不会在此类更改下重复使用,因此保持对金额的允许您正确格式化。 Additionally just because a country formats their currency amounts one way doesn't mean everyone does. 另外,仅仅因为一个国家以一种方式格式化其货币数量并不意味 Eg. 例如。 25 French Francs was be "FF25.00" in the UK but "25FF00" or "FF25,00" in other locales. 25法国法郎在英国是“FF25.00”,在其他地方是“25FF00”或“FF25,00”。 (EDIT: I note you covered this last paragraph in the question.) (编辑:我注意到你在这个问题的最后一段。)

Of course, the simplest way is to not use the locale-specific currency format, but rather to format the amount as a simple number and pre- or suffix it with the ISO currency code. 当然,最简单的方法是不使用特定于语言环境的货币格式,而是将金额格式化为简单数字,并使用ISO货币代码预先或后缀。 The convention 大会

       ------------
Sum:   ATS 1.376,00  (= EUR 100,00)

is commonly found on invoices (using locale de-AT as an example). 通常在发票上找到(使用locale de-AT作为示例)。

If you want to use the built-in currency formatting options, I would suggest to replace the currency symbol with the one stored in the database. 如果您想使用内置货币格式选项,我建议将货币符号替换为存储在数据库中的货币符号。 Ie, in your currency table, you'd need to map currencies to symbols: 即,在您的currency表中,您需要将货币映射到符号:

EUR -> €
ATS -> S
...

and then you'd replace myCultureInfo.NumberFormat.CurrencySymbol with the one in the database. 然后你用数据库中的那个替换myCultureInfo.NumberFormat.CurrencySymbol That way, you ensure that a value is never shown with the wrong currency symbol. 这样,您可以确保永远不会使用错误的货币符号显示值。

If you are targeting Windows 8 or above, this deficiency is addressed by the Windows.Globalization.NumberFormatting.CurrencyFormatter . 如果您的目标是Windows 8或更高版本,则Windows.Globalization.NumberFormatting.CurrencyFormatter将解决此缺陷。 It requires that you provide the explicit currency and you can also explicitly provide a language. 它要求您提供显式货币,您还可以明确提供语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM