views:

375

answers:

3

Hi, 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.

A: 

use this format string :

#,##0.00 $;#,##0.00'-  $';0 $
decimal paid = Convert.ToDecimal(dr["TotalPaids"]);
lblPaids.Text = paid.ToString("#,##0.00 $;#,##0.00'-  $';0 $");
masoud ramezani
+1  A: 

If I understand your question correctly what you want is to replace the $ with RM. If so, you need to pass the custom format...

lblPaids.Text = paid.ToString("C", LocalFormat);
Jonathan
Tq for the answer, but the local CultureInfo is (ms-MY).Since I'm using windows XP the currency sign for this culture is R and not RM which is the correct sign.
Agamand The True
Well then you need to change the value you are setting: LocalFormat.CurrencySymbol = "R";
Jonathan
+2  A: 

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.

Mike Two
Thanks Mike. My CultureInfo is ms-MY. But for the purpose of displaying currency value, I need to use en-US because it display correctly. E.g ms-MY = R11.747.435 and en-US = RM11,747,435.00 .Is there any other way, how about the ToString("C") method overriding. Could you please help me with that.
Agamand The True
@Agamand The True - You can either always call `ToString("C", LocalFormat)` Or change some of the other properties on the current cultures NumberFormatInfo. It seems that you just want to change the separator and number of decimal places. I'll edit the answer to include that.
Mike Two
Thanks Mike, This is perfect. But when I put this in Global.asax, Application_Start method. I get this error 'System.Globalization.CultureInfo' does not contain a definition for 'NumberFormatInfo' and no extension method 'NumberFormatInfo' accepting a first argument of type 'System.Globalization.CultureInfo' could be found (are you missing a using directive or an assembly reference?)I've already imported System.Globalization. How can I solve this....
Agamand The True
@Agamand The True - Sorry about that. The property on `CultureInfo` is called `NumberFormat` its type is `NumberFormatInfo`. My mistake. I fixed the code in the answer.
Mike Two
When I run the given code, I get another error saying Instance is read-only. I don't understand why, because NumberFormatInfo has getter and setter. Could you please explain a bit more..
Agamand The True
Sorry, one more edit
Mike Two