How to set CurrencySymbol on Readonly CultureInfo.NumberFormat?
Asked Answered
P

1

11

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.

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

Example:

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

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

Thanks!

Profiterole answered 4/6, 2009 at 16:37 Comment(0)
V
16

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

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.

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));
    }
}
Vigorous answered 4/6, 2009 at 16:40 Comment(1)
Beautiful, and simply simple. I could kick myself for missing this. Thanks!Profiterole

© 2022 - 2024 — McMap. All rights reserved.