This should be an easy problem but...
I need to format a currency for display (string) in C#
The currency in question will have its own rules such as the sym
NumberFormatInfo
from the user's currency, and clone itCurrencySymbol
in the cloned format to the CurrencySymbol
of the currency in questionCurrencyPositivePattern
and CurrencyNegativePattern
in the same way.For example:
using System;
using System.Globalization;
class Test
{
static void Main()
{
decimal total = 1234.56m;
CultureInfo vietnam = new CultureInfo(1066);
CultureInfo usa = new CultureInfo("en-US");
NumberFormatInfo nfi = usa.NumberFormat;
nfi = (NumberFormatInfo) nfi.Clone();
NumberFormatInfo vnfi = vietnam.NumberFormat;
nfi.CurrencySymbol = vnfi.CurrencySymbol;
nfi.CurrencyNegativePattern = vnfi.CurrencyNegativePattern;
nfi.CurrencyPositivePattern = vnfi.CurrencyPositivePattern;
Console.WriteLine(total.ToString("c", nfi));
}
}
Admittedly my console doesn't manage to display the right symbol, but I'm sure that's just due to font issues :)
To work with a textbox, try the following:
textBox2.Text = "Rp" + (textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(textBox1.Text)));
Sorry, I may be being a little slow here, but I still don't see your point. It seems to me that the original question is "I have a dong value which, in Vietnam, I want displayed as per Vietnamese currency format, with a "₫" symbol, and in the US, I want displayed as per US currency format, but still with a "₫".".
I guess I'm getting confused by the two contradictory statements... "The currency in question will have its own rules" and "how the numbers are formatted depends upon the users local [sic], not the currency."
If all you wanted to change was the currency symbol, and leave the formatting as per the current culture, wouldn't cloning the nfi and setting the symbol be enough ?
NumberFormatInfo nfi;
// pretend we're in Vietnam
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("vi-VN");
nfi = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;
nfi.CurrencySymbol = "₫";
String s1 = (1234.5678).ToString("c", nfi);
// pretend we're in America
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
nfi = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;
nfi.CurrencySymbol = "₫";
String s2 = (1234.5678).ToString("c", nfi);