Question

I want my values to be shown in the views

Like: -$150.00

Instead of: ($150.00)

--

I guess this is what I have to do:

How do I display a negative currency in red?

But I don't know what does he means by "BaseController class"

Was it helpful?

Solution 2

So merging @Jon Skeet answer with this one

The Real Answer was to add this method to the Global.asax.cs file of your MVC Project. And that's it.

The key is the second line:

protected void Application_BeginRequest(object sender, EventArgs e)
        {

            CultureInfo culture = new CultureInfo("en-us");
            culture.NumberFormat.CurrencyNegativePattern = 1;    

            Thread.CurrentThread.CurrentUICulture = culture;
            Thread.CurrentThread.CurrentCulture = culture;
        }

OTHER TIPS

It's all down to NumberFormatInfo.CurrencyNegativePattern. Presumably you've got the value 0, when it sounds like you want 1.

It's not clear whether you're currently using the user's CultureInfo, the server's one, or something else. But you could always clone whichever culture you're using, then modify the NumberFormatInfo.

Sample code:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        var original = new CultureInfo("en-us");
        // Prints ($5.50)
        Console.WriteLine(string.Format(original, "{0:C}", -5.50m));

        var modified = (CultureInfo) original.Clone();
        modified.NumberFormat.CurrencyNegativePattern = 1;        
        // Prints -$5.50
        Console.WriteLine(string.Format(modified, "{0:C}", -5.50m));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top