Problem with ToString("C")

Posted on 2011-05-06
Last Modified: 2013-12-17
Im having a weird problem with this method on an web page using C#. On my local system which is in the UK I use ToString("C")to get the currency of the double value  -12294403.753546 which gives me
  -£12,294,403.75 which is correct.

Now when I do the same thing on the server, which is exactly the same source code and also in the UK then it gives ($12,294,403.75)

I have no idea why its putting it in brackets, using a dollar sign, and also it doesnt give a negative result when it was given a negative number! has anyone else had this problem??

Maybe theres a better way of displaying this instead of using ToString("C")?
Hope you can help.


Question by:LukePerrin01
    LVL 53

    Expert Comment

    ToString("c") always format the dates into the culture defined on the system. To overcome this, you should give your own structure...

    See: Formatting Numeric Data for a Specific Culture
    LVL 13

    Accepted Solution


    Set the website's culture by adding the following line to the web.config:

    <globalization uiCulture="en" culture="en-gb" />
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Hi, also you can define the culture to use to format the string for that line of code:
    double myNumber = -12294403.753546;
    System.Globalization.CultureInfo ciEnUs = new System.Globalization.CultureInfo("en-GB");
    Console.WriteLine(myNumber.ToString("C", ciEnUs));

    Open in new window

    And at page level too:
    <%@ Page Language="C#" UICulture="en" Culture="en-GB" %>

    Open in new window

    LVL 21

    Expert Comment

    You could also use the CurrentCulture property to define your culture:
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
    double mynum = -1234.5678;
    //You can also go back to the original culture by storing the original culture and later use it again if you want.
    System.Globalization.CultureInfo CI = System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
    double mynum = -1234.5678;
    System.Threading.Thread.CurrentThread.CurrentCulture = CI;

    Open in new window

    LVL 33

    Expert Comment

    by:Todd Gerbert
    I suspect your server shows the U.S.-style format (by the way, representing negative monetary values in the U.S. is often done with parentheses instead of a minus "-" sign) because the "Region" control panel applet on the server is set to United States.

    If you just want to fix your server's representation check the control panel; this way clients will see whatever format's they have defined on their system.

    If you want to always force UK formats, see the posts above.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    network + 7 57
    Add json string to a javascript object 9 34
    How to perform calculation 2 21
    GridDataView 6 15
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now