Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Currency symbol and decimal places using decimal.ToString("C") and CultureInfo

I have a problem with decimal.ToString("C") override. Basically what I wants to do is as follows:

CultureInfo usCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = usCulture;

NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
LocalFormat.CurrencySymbol = "RM";

I wants to make above code a function (override ToString("C")) whereby when the following code get executed:

decimal paid = Convert.ToDecimal(dr["TotalPaids"]);
lblPaids.Text = paid.ToString("C");

The results would be RM4,900.00 instead of $4,900.00

How do I create an override for decimal.ToString("C") that would solve my problem

Thanks in advance.

like image 800
Agamand The True Avatar asked Feb 23 '10 07:02

Agamand The True


People also ask

How to change currency in c#?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.

How do you convert decimal to string?

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal.ToString(String, IFormatProvider) method.

Which of the following objects have an effect on .NET handling dates formatting and Unformatting issues currencies etc?

CurrentCulture handles dates,currencies,sorting and formatting issues in Dot Net.


1 Answers

To get a format like RM 11,123,456.00 you also need to set the following properties

CurrentCulture modified = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
Thread.CurrentThread.CurrentCulture = modified;
var numberFormat = modified.NumberFormat;
numberFormat.CurrencySymbol = "RM";
numberFormat.CurrencyDecimalDigits = 2;
numberFormat.CurrencyDecimalSeparator = ".";
numberFormat.CurrencyGroupSeparator = ",";

If you do that at application startup then that should make ms-MY format like en-US but with the RM currency symbol every time you call the ToString("C") method.

like image 174
Mike Two Avatar answered Oct 28 '22 01:10

Mike Two