C# 给定货币代码格式化货币(如 USD / GBP / FRF)

发布于 2024-10-17 19:01:29 字数 402 浏览 7 评论 0原文

我正在与返回货币 (System.Decimal) 和货币代码的数据库集成。货币代码是诸如 "USD""GBP" 甚至 "FRF" 之类的字符串。

mscorlib 是否有内置的东西可以帮助我格式化这些货币?我首先考虑在数据库货币代码和 CultureInfo 之间建立映射,但我不知道如何处理 FRF,因为如果我使用 "fr-FR",它将格式化为欧元而不是法郎。

我们必须支持的货币符号的完整列表是:

FRF
CHF
NZD
IN2
SAR
SEK
EUR
MXP
DKK
GBP
AUD
IN1
AED
CAD
NOK
INR
USD
PLN

I am integrating with a database which returns currencies (System.Decimal) and currency codes. The currency codes are strings like "USD", "GBP", and even "FRF".

Is there something built-in to mscorlib which can help me format these currencies? I first thought about setting up a mapping between database currency code and CultureInfo, but I don't know what to do about FRF because if I use "fr-FR", it would format as euros instead of francs.

The full list of currency symbols we must support is:

FRF
CHF
NZD
IN2
SAR
SEK
EUR
MXP
DKK
GBP
AUD
IN1
AED
CAD
NOK
INR
USD
PLN

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

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

发布评论

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

评论(3

找个人就嫁了吧 2024-10-24 19:01:29

我首先考虑在数据库货币代码和 CultureInfo 之间建立映射,但我不知道如何处理 FRF,因为如果我使用“fr-FR”,它将格式化为欧元而不是法郎。

我会采用这种方法,但为 FRF 制作一个自定义 IFormatProvider 来执行您选择的格式。这就像正确构建 NumberFormatInfo 一样简单。

I first thought about setting up a mapping between database currency code and CultureInfo, but I don't know what to do about FRF because if I use "fr-FR", it would format as euros instead of francs.

I would take this approach, but make a custom IFormatProvider for FRF that does the formatting you choose. This is as easy as constructing a NumberFormatInfo properly.

云朵有点甜 2024-10-24 19:01:29

您可以替换默认货币并仍然使用该框架...

using System;
using System.Globalization;
using System.Threading;
public class EuroLocalSample
{
   public static void Main()
   { 
     // Create a CultureInfo object for French in France.
     CultureInfo FrCulture = new CultureInfo("fr-FR");
     // Set the CurrentCulture to fr-FR.
     Thread.CurrentThread.CurrentCulture = FrCulture;
    // Clone the NumberFormatInfo object and create
    // a new object for the local currency of France.
    NumberFormatInfo LocalFormat =
    (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
    // Replace the default currency symbol with the local
    // currency symbol.
    LocalFormat.CurrencySymbol = "F";

    int i = 100;

    // Display i formatted as the local currency.
    Console.WriteLine(i.ToString("c", LocalFormat));
    // Display i formatted as the default currency.
    Console.WriteLine(i.ToString("c", NumberFormatInfo.CurrentInfo));
   }
}

You can replace the default currency and still make use of the framework...

using System;
using System.Globalization;
using System.Threading;
public class EuroLocalSample
{
   public static void Main()
   { 
     // Create a CultureInfo object for French in France.
     CultureInfo FrCulture = new CultureInfo("fr-FR");
     // Set the CurrentCulture to fr-FR.
     Thread.CurrentThread.CurrentCulture = FrCulture;
    // Clone the NumberFormatInfo object and create
    // a new object for the local currency of France.
    NumberFormatInfo LocalFormat =
    (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
    // Replace the default currency symbol with the local
    // currency symbol.
    LocalFormat.CurrencySymbol = "F";

    int i = 100;

    // Display i formatted as the local currency.
    Console.WriteLine(i.ToString("c", LocalFormat));
    // Display i formatted as the default currency.
    Console.WriteLine(i.ToString("c", NumberFormatInfo.CurrentInfo));
   }
}
鹿童谣 2024-10-24 19:01:29

这是一个很小的清单。我只是手动设置映射。

That's a pretty small list. I'd just setup the mappings manually.

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