Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Convert to double problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Yoavo - 26 Sep 2007 10:37 GMT
Hi,
I want to convert a string to double. I use the function:
"System.Convert.ToDouble".
The problem is that if the string contains the character "." the program
aborts.

What might be the problem ?

Yoav.
Jon Skeet [C# MVP] - 26 Sep 2007 11:59 GMT
> I want to convert a string to double. I use the function:
> "System.Convert.ToDouble".
> The problem is that if the string contains the character "." the program
> aborts.
>
> What might be the problem ?

Chances are you're using a culture which doesn't use "." as the
decimal separator.

Try specifying CultureInfo.InvariantCulture as the culture in the
Convert.ToDouble call.

Jon
Göran Andersson - 26 Sep 2007 12:00 GMT
> Hi,
> I want to convert a string to double. I use the function:
[quoted text clipped - 5 lines]
>
> Yoav.

The ToDouble method is using a NumberFormat object that specifies
different characters for decimal separator and thousands separator than
what you have in the string.

The method uses the NumberFormat of the CultureInfo.CurrentCulture object.

You can use the Double.Parse method instead, so that you can specify the
 FormatInfo or CultureInfo object directly. For a string that uses a
period as decimal separator, you can use the InvariantCulture object:

string s = "2.1415926536";
double d = double.Parse(s, CultureInfo.InvariantCulture);

Signature

Göran Andersson
_____
http://www.guffa.com

Yoavo - 30 Sep 2007 06:58 GMT
I found what was the problem.
I was using "," as decimal point instad of "."
Isn't this function should work OK if I am using "," as decimal point ??

> Hi,
> I want to convert a string to double. I use the function:
[quoted text clipped - 5 lines]
>
> Yoav.
Göran Andersson - 30 Sep 2007 11:54 GMT
> I found what was the problem.
> I was using "," as decimal point instad of "."
> Isn't this function should work OK if I am using "," as decimal point ??

It works just fine if you specify a FormatInfo or CultureInfo that
correctly describes how you write the number. Example:

string s = "3,1415926536";
CultureInfo swedish = new CultureInfo(1053);
double d = double.Parse(s, swedish);

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.