473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double conversion problem

Hi there,

I'm having a weird situation when converting strings to double.
Normally, the following code would work right?

double MyDouble = double.Parse("2 0.50"); // Error at runtime

Well, it gives me an error at run time, telling me that the format of
the input string is incorrect. Then, if i try with a comma instead of
a dot, it works fine.

double MyDouble = double.Parse("2 0,50"); // Works fine

Isn't it supposed to be a dot that works? I think it could be related
to "Regional and Language Options" in WinXP. I'm using french canadian
regional options if it can gives you a hint. And if it is really
related to regional and language options, what should i change
exactly?

If anyone has an idea how to make it works with a dot that would be
great. I want understand why it doesn't works. Thanks!
Nov 15 '05 #1
4 17783
I'm sure it is your regional settings.

You will notice that in the Regional Options Tab (in the Regional and
Language Options) the samples will show you how the number is to be
formatted. For French Canadian, a number is this:

"123 456 789,00"

which would explain your error. Change the format to English and you won't
have the problem.

If you must have the french Canadian, then you have to use that number
format, where a comma is the decimal separator. You could also customize
and change the decimal to a period.

Hope this helps.
"Matt" <me***@rocks.co m> wrote in message
news:ki******** *************** *********@4ax.c om...
Hi there,

I'm having a weird situation when converting strings to double.
Normally, the following code would work right?

double MyDouble = double.Parse("2 0.50"); // Error at runtime

Well, it gives me an error at run time, telling me that the format of
the input string is incorrect. Then, if i try with a comma instead of
a dot, it works fine.

double MyDouble = double.Parse("2 0,50"); // Works fine

Isn't it supposed to be a dot that works? I think it could be related
to "Regional and Language Options" in WinXP. I'm using french canadian
regional options if it can gives you a hint. And if it is really
related to regional and language options, what should i change
exactly?

If anyone has an idea how to make it works with a dot that would be
great. I want understand why it doesn't works. Thanks!

Nov 15 '05 #2
"Matt" <me***@rocks.co m> wrote in message
news:ki******** *************** *********@4ax.c om...
double MyDouble = double.Parse("2 0.50"); // Error at runtime

Well, it gives me an error at run time, telling me that the format of
the input string is incorrect. Then, if i try with a comma instead of
a dot, it works fine.

double MyDouble = double.Parse("2 0,50"); // Works fine

Isn't it supposed to be a dot that works? I think it could be related
to "Regional and Language Options" in WinXP. I'm using french canadian
regional options if it can gives you a hint. And if it is really
related to regional and language options, what should i change
exactly?

If anyone has an idea how to make it works with a dot that would be
great. I want understand why it doesn't works. Thanks!


Try:
double.Parse("2 0.50", new System.Globaliz ation.CultureIn fo("en-US"));

And yes, it's about regional options... :)
Nov 15 '05 #3
Well it was the regional settings. I just set it to Canadian English
and it works fine for both.

However it means that this section of code will depend of the user's
regional settings. Fortunately for me it's an asp.net project that
will run on a specific server. But if it was a c# program made for
deployment on different computers, that would be problematic.
Nov 15 '05 #4
Matt,

Everyone has this number from string parse problem. As an American
living in Europe, with a french computer programing for the US, I got
to it quicker. If you need to reliably convert a number to and from a
string, there is nothing better than "forcing" the culture to
CultureInfo.Inv ariantCulture. You will find it (if not exactly) very
similar to en-US. so parsing would go like:
double dbl = double.Parse("1 234.56",
System.Globaliz ation.CultureIn fo.InvariantCul ture);

And when you need to get your string from the double, you can do the
same thing in "reverse":
string s =
dbl.ToString(Sy stem.Globalizat ion.CultureInfo .InvariantCultu re);

Scott

"Matt" <me***@rocks.co m> wrote in message
news:s9******** *************** *********@4ax.c om...
Well it was the regional settings. I just set it to Canadian English
and it works fine for both.

However it means that this section of code will depend of the user's
regional settings. Fortunately for me it's an asp.net project that
will run on a specific server. But if it was a c# program made for
deployment on different computers, that would be problematic.

Nov 15 '05 #5

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

Similar topics

11
2511
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a random number generator in the context of a mixed Python/C++ programming problem. I encountered a vexing type conversion problem.
5
1845
by: ann | last post by:
Does somebody know why I get a blank string in strA? Last time post the wrong code. " Option Strict On Option Explicit On Public Class Cast Private Sub FuncA()
3
6067
by: Ice Man | last post by:
Hi I want to send e-mails using an asp page first I create the mail Object: Set objMail = Server.CreateObject("CDONTS.NewMail") After I fill a variable with the html code that will be the mail txtMailBody = "<html>" txtMailBody = txtMailBody & "<center>Hi <BR> This is a test"
2
2330
by: Darryl A. J. Staflund | last post by:
Hi there, Can anyone tell me why invoking a single SQL insert statement (well, rather, a method that performs a SQL insert) using an asynchronous delegate should result in twice the number of records being inserted? Am I just making a mistake somewhere? (though I don't get double the records if I call the method synchronously.) Thanks,...
1
2632
by: Michael Tissington | last post by:
I'm trying to convert a project from VS2003 to VS2005 After conversion all of my TagPrefix are not recognized in the body. <%@ Register TagPrefix="Oaklodge" TagName="Curve" Src="ctrls/Curve.ascx" %> This does not report any errors, but when I try to use it like <Oaklodge:Curve runat="server" /> I get the following error
0
1111
by: egbert.beuker | last post by:
Hi, I encountered a conversion problem in my .net web app (c#), and I hope someone can help me: I'm working on a generic way to store data in a database with a few generated classes. I want to loop through posted values, store them in properties in a class, and then save the data with the class function "Update", which by the way works...
0
2390
by: ipoxygen | last post by:
Hi, I do have 6 identical tables on six different databases (same server). I would like to merge them into one single table for reporting purposes. For the majority of the table it does work without a problem with following procedure: /* 100: NL 200: BE 300: UK 400: FR 600: SP
15
4944
by: ABC | last post by:
Hi, I have a problem with Double submission in html forms. I am using PHP as the server side scripting language, i have found a means to capture the second form submission while the first form submission is still in-progress. Basically, i can use sessions to check if the first submission is active and reject subsequent submits. ***I do...
0
1559
by: santhescript01 | last post by:
Unicode to non unicode conversion problem -------------------------------------------------------------------------------- Hi All, I am using C dll in macro which converts Unicode data to 8 bit encoding data ' Prototype of C function. ' extern "C" int _stdcall Uni2Eni(wchar_t * uni, unsigned char * eni, int size)
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7766
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.