如何使用CultureInfo格式化已过时的货币? [英] How to use CultureInfo to format deprecated currencies?

查看:182
本文介绍了如何使用CultureInfo格式化已过时的货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在dotnet中,以文化特定方式格式化货币的推荐方式是(据我所知):

 使用System.Globalization 
var info = CultureInfo.GetCultureInfo(en-GB)
返回string.Format(info,{0:c},1234.5678)

返回:

 £1,234.57 

然而,这里没有特定的货币。因此,如果英国曾经转换为欧元,那么同样的方法会突然返回:

 €1,234.57 

code>

刚刚发生在拉脱维亚。今年,它转换为欧元。各种系统都含有以莱索特和欧元计的金额。我需要能够继续打印和格式化旧货币。而且我需要确定新货币也可以处理。这个格式不只是关于符号,而且关于放置(之前,之后,有没有空格)和本地数字格式(小数点分隔符和千位分隔符)。

<在我们的数据库中的每一个金额都有一个货币(如欧元)和相应的文化(如NLL)。欧元金额的格式有所不同,取决于它们是源于我们的德国还是我们的荷兰办事处。 (他们都使用欧元,但格式不同)


  • 是否允许访问较旧的货币?




现在,我能想到的唯一方法就是,就是将文化数据库复制到我的数据库中。

msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.110%29.aspxrel =nofollow> CultureInfo (通过一个和修改),它使用不同的货币符号/格式(即设置其 NumberFormat 到另外一个 NumberFormatInfo )。



然后传递这个 CultureInfo 来根据需要格式化函数。



到问题注释,.NET(和Windows一般)不提供历史数据(类似于时区,但有一个图书馆)。在需要的情况下,您需要自己保存足够的数据。



请记住 ISO-4217 货币代码在这样的变化下不会被重复使用,所以在数量上保持不变,就可以正确格式化。此外,仅仅因为一个国家的货币形式不一定意味着每个人都这样做。例如。英国的25法郎在英国是FF25.00,在其他地方是25FF00或FF25,00。 (编辑:我注意到你在这个问题的最后一段。)


In dotnet, the recommended way of formatting currencies in a culture-specific way, is (as far as I can find):

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. 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 (e.g. EUR) and a corresponding culture (e.g. 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?
  • 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.

解决方案

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

Then pass this CultureInfo to formatting functions as needed.

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). 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. 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. (EDIT: I note you covered this last paragraph in the question.)

这篇关于如何使用CultureInfo格式化已过时的货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆