如何在只读CultureInfo.NumberFormat设置CurrencySymbol?如何在、CultureInfo、CurrencySymbol、NumberFormat

2023-09-03 06:43:40 作者:残影君醉相思浓

我试图格式化货币(瑞士弗兰克 - DE-CH)用一个符号(CHF)是不同的,默认的.Net文化是什么(SFR)。问题是,对于NumberFormat的文化是只读的。

I'm trying to format a currency (Swiss Frank -- de-CH) with a symbol (CHF) that is different that what the default .Net culture is (SFr.). The problem is that the NumberFormat for the culture is ReadOnly.

有没有一种简单的方法来解决使用的CultureInfo和NumberFormat的这个问题?有一些方法可以让我重写CurrencySymbol?

Is there a simple way to solve this problem using CultureInfo and NumberFormat? Is there some way I can override the CurrencySymbol?

例: 昏暗newCInfo作为CultureInfo的= CultureInfo.GetCultureInfo(2055) newCInfo.NumberFormat.CurrencySymbol =瑞士法郎 MyCurrencyText.Text = x.ToString(C,newCInfo)

Example: Dim newCInfo As CultureInfo = CultureInfo.GetCultureInfo(2055) newCInfo.NumberFormat.CurrencySymbol = "CHF" MyCurrencyText.Text = x.ToString("c",newCInfo)

这会报错关于设置NumberFormat.CurrencySymbol因为NumberFormat的是只读的。

This will error on setting the NumberFormat.CurrencySymbol because NumberFormat is ReadOnly.

谢谢!

推荐答案

呼叫克隆的CultureInfo ,其中将创建一个可变的版本,然后将货币符号。

Call Clone on the CultureInfo, which will create a mutable version, then set the currency symbol.

您可以更具体:取的NumberFormatInfo ,只有克隆的。这是稍微更优雅,国际海事组织,除非你需要改变任何东西的文化。

You could be more specific: fetch the NumberFormatInfo and only clone that. It's slightly more elegant, IMO, unless you need to change anything else in the culture.

实例在C#:

Example in C#:

using System;

using System.Globalization;

class Test
{
    static void Main()
    {
        CultureInfo original = CultureInfo.GetCultureInfo(2055);
        NumberFormatInfo mutableNfi = (NumberFormatInfo) original.NumberFormat.Clone();
        mutableNfi.CurrencySymbol = "X";
        Console.WriteLine(50.ToString("C", mutableNfi));
    }
}