Input string was not in a correct format #2
double temp;
temp = (double)Convert.ToDouble("1234.5678");
Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;
An unhandled exception of type
System.FormatException occurred in
mscorlib.dll
Additional information: Input string
was not in a correct format.
c# .net parsing double
add a comment |
double temp;
temp = (double)Convert.ToDouble("1234.5678");
Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;
An unhandled exception of type
System.FormatException occurred in
mscorlib.dll
Additional information: Input string
was not in a correct format.
c# .net parsing double
4
+1 for hey ladies ;)
– Armance
Dec 15 '11 at 17:06
add a comment |
double temp;
temp = (double)Convert.ToDouble("1234.5678");
Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;
An unhandled exception of type
System.FormatException occurred in
mscorlib.dll
Additional information: Input string
was not in a correct format.
c# .net parsing double
double temp;
temp = (double)Convert.ToDouble("1234.5678");
Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;
An unhandled exception of type
System.FormatException occurred in
mscorlib.dll
Additional information: Input string
was not in a correct format.
c# .net parsing double
c# .net parsing double
edited Dec 24 '12 at 20:00
John Saunders
147k22204362
147k22204362
asked Mar 11 '11 at 16:11
Keith LoughnaneKeith Loughnane
1601117
1601117
4
+1 for hey ladies ;)
– Armance
Dec 15 '11 at 17:06
add a comment |
4
+1 for hey ladies ;)
– Armance
Dec 15 '11 at 17:06
4
4
+1 for hey ladies ;)
– Armance
Dec 15 '11 at 17:06
+1 for hey ladies ;)
– Armance
Dec 15 '11 at 17:06
add a comment |
10 Answers
10
active
oldest
votes
As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
add a comment |
In order to convert string to double without an exception:
An unhandled exception of type System.FormatException occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:
double.Parse("1234.5678", CultureInfo.InvariantCulture)
add a comment |
first solution (as mentioned in other posts):
double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);
second solution - make it default to current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
double temp = double.Parse("1234.5678");
third solution - make it default to block of code:
var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
...
double temp = double.Parse("1234.5678");
...
Thread.CurrentThread.CurrentCulture = prevCurrentCulture;
add a comment |
You may be somehow using a european local. In some countries the . and , in numbers is reversed.
add a comment |
Hi as Mario says you must parse it taking into account the regional settings.
double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);
Regards.
add a comment |
Check your regional settings. Your decimal symbol needs to be ".".
add a comment |
double temp = double.Parse("1234,5678");
add a comment |
I recommend you use TryParse instead, so you don't need to handle parsing exceptions.
double temp = 0;
if (double.TryParse("123.456", out temp)
{
Console.WriteLine(string.Format("Parsed temp: {0}", temp);
}
else
{
Console.WriteLine("Input value was not able to be parsed.");
}
add a comment |
I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.
add a comment |
I found the problem when you let the text box empty then
this error occurs so try this one to handle it.
An unhandled exception of type System.FormatException occurred in
mscorlib.dll Additional information: Input string was not in a correct
format.
if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5275380%2finput-string-was-not-in-a-correct-format-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
add a comment |
As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
add a comment |
As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.
As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.
answered Mar 11 '11 at 16:16
MarioMario
29.3k44165
29.3k44165
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
add a comment |
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
Thanks, That was it.
– Keith Loughnane
Mar 14 '11 at 13:07
add a comment |
In order to convert string to double without an exception:
An unhandled exception of type System.FormatException occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:
double.Parse("1234.5678", CultureInfo.InvariantCulture)
add a comment |
In order to convert string to double without an exception:
An unhandled exception of type System.FormatException occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:
double.Parse("1234.5678", CultureInfo.InvariantCulture)
add a comment |
In order to convert string to double without an exception:
An unhandled exception of type System.FormatException occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:
double.Parse("1234.5678", CultureInfo.InvariantCulture)
In order to convert string to double without an exception:
An unhandled exception of type System.FormatException occurred in
mscorlib.dll
Additional information: Input string was not in a correct format.
make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:
double.Parse("1234.5678", CultureInfo.InvariantCulture)
edited Nov 28 '13 at 11:52
answered Dec 24 '12 at 19:41
Vadim GremyachevVadim Gremyachev
35k770113
35k770113
add a comment |
add a comment |
first solution (as mentioned in other posts):
double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);
second solution - make it default to current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
double temp = double.Parse("1234.5678");
third solution - make it default to block of code:
var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
...
double temp = double.Parse("1234.5678");
...
Thread.CurrentThread.CurrentCulture = prevCurrentCulture;
add a comment |
first solution (as mentioned in other posts):
double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);
second solution - make it default to current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
double temp = double.Parse("1234.5678");
third solution - make it default to block of code:
var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
...
double temp = double.Parse("1234.5678");
...
Thread.CurrentThread.CurrentCulture = prevCurrentCulture;
add a comment |
first solution (as mentioned in other posts):
double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);
second solution - make it default to current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
double temp = double.Parse("1234.5678");
third solution - make it default to block of code:
var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
...
double temp = double.Parse("1234.5678");
...
Thread.CurrentThread.CurrentCulture = prevCurrentCulture;
first solution (as mentioned in other posts):
double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);
second solution - make it default to current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
double temp = double.Parse("1234.5678");
third solution - make it default to block of code:
var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
...
double temp = double.Parse("1234.5678");
...
Thread.CurrentThread.CurrentCulture = prevCurrentCulture;
answered Dec 15 '14 at 11:11
BachorBachor
41079
41079
add a comment |
add a comment |
You may be somehow using a european local. In some countries the . and , in numbers is reversed.
add a comment |
You may be somehow using a european local. In some countries the . and , in numbers is reversed.
add a comment |
You may be somehow using a european local. In some countries the . and , in numbers is reversed.
You may be somehow using a european local. In some countries the . and , in numbers is reversed.
answered Mar 11 '11 at 16:17
Steve WellensSteve Wellens
19.1k22054
19.1k22054
add a comment |
add a comment |
Hi as Mario says you must parse it taking into account the regional settings.
double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);
Regards.
add a comment |
Hi as Mario says you must parse it taking into account the regional settings.
double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);
Regards.
add a comment |
Hi as Mario says you must parse it taking into account the regional settings.
double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);
Regards.
Hi as Mario says you must parse it taking into account the regional settings.
double temp = double.Parse("1234.5678", System.Globalization.CultureInfo.CurrentCulture);
Regards.
answered Mar 11 '11 at 16:23
Marco MedranoMarco Medrano
584
584
add a comment |
add a comment |
Check your regional settings. Your decimal symbol needs to be ".".
add a comment |
Check your regional settings. Your decimal symbol needs to be ".".
add a comment |
Check your regional settings. Your decimal symbol needs to be ".".
Check your regional settings. Your decimal symbol needs to be ".".
answered Mar 11 '11 at 16:16
Tertius GeldenhuysTertius Geldenhuys
352214
352214
add a comment |
add a comment |
double temp = double.Parse("1234,5678");
add a comment |
double temp = double.Parse("1234,5678");
add a comment |
double temp = double.Parse("1234,5678");
double temp = double.Parse("1234,5678");
answered Mar 11 '11 at 16:14
AchilleterzoAchilleterzo
686514
686514
add a comment |
add a comment |
I recommend you use TryParse instead, so you don't need to handle parsing exceptions.
double temp = 0;
if (double.TryParse("123.456", out temp)
{
Console.WriteLine(string.Format("Parsed temp: {0}", temp);
}
else
{
Console.WriteLine("Input value was not able to be parsed.");
}
add a comment |
I recommend you use TryParse instead, so you don't need to handle parsing exceptions.
double temp = 0;
if (double.TryParse("123.456", out temp)
{
Console.WriteLine(string.Format("Parsed temp: {0}", temp);
}
else
{
Console.WriteLine("Input value was not able to be parsed.");
}
add a comment |
I recommend you use TryParse instead, so you don't need to handle parsing exceptions.
double temp = 0;
if (double.TryParse("123.456", out temp)
{
Console.WriteLine(string.Format("Parsed temp: {0}", temp);
}
else
{
Console.WriteLine("Input value was not able to be parsed.");
}
I recommend you use TryParse instead, so you don't need to handle parsing exceptions.
double temp = 0;
if (double.TryParse("123.456", out temp)
{
Console.WriteLine(string.Format("Parsed temp: {0}", temp);
}
else
{
Console.WriteLine("Input value was not able to be parsed.");
}
answered Mar 11 '11 at 16:17
Mike AtlasMike Atlas
7,00333456
7,00333456
add a comment |
add a comment |
I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.
add a comment |
I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.
add a comment |
I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.
I don't see any problem with the above code.it works fine and prints the value 1234.5678. I have tried it in VS2008. Probably, something to do with locale settings on your machine.
edited Mar 11 '11 at 16:20
answered Mar 11 '11 at 16:15
KumarKumar
97447
97447
add a comment |
add a comment |
I found the problem when you let the text box empty then
this error occurs so try this one to handle it.
An unhandled exception of type System.FormatException occurred in
mscorlib.dll Additional information: Input string was not in a correct
format.
if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}
add a comment |
I found the problem when you let the text box empty then
this error occurs so try this one to handle it.
An unhandled exception of type System.FormatException occurred in
mscorlib.dll Additional information: Input string was not in a correct
format.
if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}
add a comment |
I found the problem when you let the text box empty then
this error occurs so try this one to handle it.
An unhandled exception of type System.FormatException occurred in
mscorlib.dll Additional information: Input string was not in a correct
format.
if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}
I found the problem when you let the text box empty then
this error occurs so try this one to handle it.
An unhandled exception of type System.FormatException occurred in
mscorlib.dll Additional information: Input string was not in a correct
format.
if (!string.IsNullOrEmpty(Txt1.Text)) {int _qty = (int)Convert.ToInt32(Txt1.Text);}
edited Nov 22 '18 at 9:04
Moritz
57.6k19131184
57.6k19131184
answered Nov 22 '18 at 8:58
Tahir ShahzadTahir Shahzad
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5275380%2finput-string-was-not-in-a-correct-format-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
+1 for hey ladies ;)
– Armance
Dec 15 '11 at 17:06