Problem with ToString("C")

Posted on 2011-05-06
Medium Priority
Last Modified: 2013-12-17
Im having a weird problem with this method on an asp.net 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

ID: 35705574
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

iHadi earned 2000 total points
ID: 35705615

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
ID: 35706040
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

by:Alfred A.
ID: 35707277
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
ID: 35711170
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

757 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