locked
How to get the negative value in US Culture RRS feed

  • Question

  • In the console application- when we give Culture as ("en-US"), getting output as ($90.31). Whereas if culture is given as ("en-GB"), getting output as -£90.31. How to get the negative value in US culture.
    Wednesday, April 8, 2020 11:30 AM

Answers

  • See if this will work for you

    var price = -90.31m;
    var usCulture = new CultureInfo("en-US") {NumberFormat = {CurrencyNegativePattern = 1}};
    Thread.CurrentThread.CurrentCulture = usCulture;
    
    var unitedStates = price.ToString("C", usCulture);
    Console.WriteLine(unitedStates);
    var ukCulture = new CultureInfo("en-GB");
    Thread.CurrentThread.CurrentCulture = ukCulture;
    var unitedKingdom = price.ToString("C", ukCulture);
    Console.WriteLine(unitedKingdom);
    
    Thread.CurrentThread.CurrentCulture = usCulture;
    Console.WriteLine(price.ToString("C", usCulture));

    -$90.31
    -£90.31
    -$90.31


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 8, 2020 12:24 PM

All replies

  • See if this will work for you

    var price = -90.31m;
    var usCulture = new CultureInfo("en-US") {NumberFormat = {CurrencyNegativePattern = 1}};
    Thread.CurrentThread.CurrentCulture = usCulture;
    
    var unitedStates = price.ToString("C", usCulture);
    Console.WriteLine(unitedStates);
    var ukCulture = new CultureInfo("en-GB");
    Thread.CurrentThread.CurrentCulture = ukCulture;
    var unitedKingdom = price.ToString("C", ukCulture);
    Console.WriteLine(unitedKingdom);
    
    Thread.CurrentThread.CurrentCulture = usCulture;
    Console.WriteLine(price.ToString("C", usCulture));

    -$90.31
    -£90.31
    -$90.31


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 8, 2020 12:24 PM
  • Hi Sujatha Babu, 
    You can set NumberFormatInfo.CurrencyNegativePattern property to 1, its associated pattern is "-$n".
    And use CultureInfo.CreateSpecificCulture() method to create a en-US or en-GB cultureInfo.
    Here is a simple code example you can refer to.

    static void Main(string[] args)
    {
        Decimal dec = new Decimal(-90.31);
        CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
        culture.NumberFormat.CurrencyNegativePattern = 1;
        String str = String.Format(culture, "{0:C}", dec);
        Console.Write(str);
        Console.ReadKey();
    }

    Output:

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 9, 2020 3:11 AM
  • Hi Sujatha Babu,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 13, 2020 6:26 AM