473,388 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Type conversion problem

This should be simple, but either I'm blind, stupid or going mad.

I'm trying to convert a string, containing a numeric value to a double (in
C#), but when I run the code below I get the error message "Input string was
not in a correct format." Why?

string s = "8.0";
double d = Convert.ToDouble(s);

Jul 22 '05 #1
4 1167

"henfo" <he***@discussions.microsoft.com> wrote in message
news:85**********************************@microsof t.com...
This should be simple, but either I'm blind, stupid or going mad.

I'm trying to convert a string, containing a numeric value to a double (in
C#), but when I run the code below I get the error message "Input string
was
not in a correct format." Why?

string s = "8.0";
double d = Convert.ToDouble(s);


The code you posted here works fine for me in the context of a simple
console application in Visual Studio 2003. Please describe the environment
surrounding the failure.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Jul 22 '05 #2
Thanks for your reply.

I too am working in Visual Studio 2003 and tried the code lines in a
console application before posting. I installed the software a couple of
months ago and have been working in it more or less daily since then and
haven't run in to any other strange problems.

\Henrik

"Peter van der Goes" wrote:

The code you posted here works fine for me in the context of a simple
console application in Visual Studio 2003. Please describe the environment
surrounding the failure.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Jul 22 '05 #3
henfo <he***@discussions.microsoft.com> wrote:
This should be simple, but either I'm blind, stupid or going mad.

I'm trying to convert a string, containing a numeric value to a double (in
C#), but when I run the code below I get the error message "Input string was
not in a correct format." Why?

string s = "8.0";
double d = Convert.ToDouble(s);


I suspect the problem is the locale you're running in. Try this:

using System;
using System.Threading;
using System.Globalization;

public class Test
{
static void Main()
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en");
Console.WriteLine (Convert.ToDouble("8.0"));
}
}

Now change the "en" to "fr" and run it again, and you'll see the error.

If you want to do the conversion in the invariant culture, do:

Convert.ToDouble(s, CultureInfo.InvariantCulture);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 22 '05 #4
Have you also tried s = "8,0";
(Region settings??)

"henfo" <he***@discussions.microsoft.com> schreef in bericht
news:85**********************************@microsof t.com...
This should be simple, but either I'm blind, stupid or going mad.

I'm trying to convert a string, containing a numeric value to a double (in
C#), but when I run the code below I get the error message "Input string
was
not in a correct format." Why?

string s = "8.0";
double d = Convert.ToDouble(s);

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
11
by: RWC | last post by:
Hello, I'm having trouble converting code in Access XP / 2002. I have some code that declares an variable "as database" in Access 97, which is not recognized in Access XP. I've tried to find a...
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.