How do I convert a string to double using input? [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Converting string to double in C#
5 answers
I want to ask a user to input a number in a string type (because that's the only way in C#) and then convert it into a double type.
private static string InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Convert.ToDouble(Console.ReadLine());
}
I hope anyone has a solution for this.
c# type-conversion
marked as duplicate by Uwe Keim, Kirk Larkin, HimBromBeere, mason
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
Converting string to double in C#
5 answers
I want to ask a user to input a number in a string type (because that's the only way in C#) and then convert it into a double type.
private static string InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Convert.ToDouble(Console.ReadLine());
}
I hope anyone has a solution for this.
c# type-conversion
marked as duplicate by Uwe Keim, Kirk Larkin, HimBromBeere, mason
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5
Your method should returndouble
– Backs
Jan 3 at 10:08
1
What is problem? (other than: your method should return double).
– isaeid
Jan 3 at 10:12
1
Ohh lol, didn't see that -_- thanks!
– Rammah
Jan 3 at 10:12
Remember to catch exceptions likeFormatException
for example.
– Valentin P
Jan 3 at 10:14
@ValentinP Which is a really bad appraoch to prevent whrongly formatted input from being parsed. Better would be usingTryParse
. Don´t catch exceptions when you can avoid them in the first place.
– HimBromBeere
Jan 3 at 10:15
|
show 1 more comment
This question already has an answer here:
Converting string to double in C#
5 answers
I want to ask a user to input a number in a string type (because that's the only way in C#) and then convert it into a double type.
private static string InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Convert.ToDouble(Console.ReadLine());
}
I hope anyone has a solution for this.
c# type-conversion
This question already has an answer here:
Converting string to double in C#
5 answers
I want to ask a user to input a number in a string type (because that's the only way in C#) and then convert it into a double type.
private static string InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Convert.ToDouble(Console.ReadLine());
}
I hope anyone has a solution for this.
This question already has an answer here:
Converting string to double in C#
5 answers
c# type-conversion
c# type-conversion
edited Jan 3 at 10:09
Uwe Keim
27.7k32135216
27.7k32135216
asked Jan 3 at 10:07


RammahRammah
102
102
marked as duplicate by Uwe Keim, Kirk Larkin, HimBromBeere, mason
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Uwe Keim, Kirk Larkin, HimBromBeere, mason
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 15:02
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5
Your method should returndouble
– Backs
Jan 3 at 10:08
1
What is problem? (other than: your method should return double).
– isaeid
Jan 3 at 10:12
1
Ohh lol, didn't see that -_- thanks!
– Rammah
Jan 3 at 10:12
Remember to catch exceptions likeFormatException
for example.
– Valentin P
Jan 3 at 10:14
@ValentinP Which is a really bad appraoch to prevent whrongly formatted input from being parsed. Better would be usingTryParse
. Don´t catch exceptions when you can avoid them in the first place.
– HimBromBeere
Jan 3 at 10:15
|
show 1 more comment
5
Your method should returndouble
– Backs
Jan 3 at 10:08
1
What is problem? (other than: your method should return double).
– isaeid
Jan 3 at 10:12
1
Ohh lol, didn't see that -_- thanks!
– Rammah
Jan 3 at 10:12
Remember to catch exceptions likeFormatException
for example.
– Valentin P
Jan 3 at 10:14
@ValentinP Which is a really bad appraoch to prevent whrongly formatted input from being parsed. Better would be usingTryParse
. Don´t catch exceptions when you can avoid them in the first place.
– HimBromBeere
Jan 3 at 10:15
5
5
Your method should return
double
– Backs
Jan 3 at 10:08
Your method should return
double
– Backs
Jan 3 at 10:08
1
1
What is problem? (other than: your method should return double).
– isaeid
Jan 3 at 10:12
What is problem? (other than: your method should return double).
– isaeid
Jan 3 at 10:12
1
1
Ohh lol, didn't see that -_- thanks!
– Rammah
Jan 3 at 10:12
Ohh lol, didn't see that -_- thanks!
– Rammah
Jan 3 at 10:12
Remember to catch exceptions like
FormatException
for example.– Valentin P
Jan 3 at 10:14
Remember to catch exceptions like
FormatException
for example.– Valentin P
Jan 3 at 10:14
@ValentinP Which is a really bad appraoch to prevent whrongly formatted input from being parsed. Better would be using
TryParse
. Don´t catch exceptions when you can avoid them in the first place.– HimBromBeere
Jan 3 at 10:15
@ValentinP Which is a really bad appraoch to prevent whrongly formatted input from being parsed. Better would be using
TryParse
. Don´t catch exceptions when you can avoid them in the first place.– HimBromBeere
Jan 3 at 10:15
|
show 1 more comment
3 Answers
3
active
oldest
votes
You can use Double's TryParse
method:
public Double? StringToDouble(String input){
if(Double.TryParse(input, out Double d)) {
Console.WriteLine("The double value is {0}", d);
return d;
}
else{
Console.WriteLine("The input string was not in correct format");
}
return null;
}
The advantage of the TryParse
method over the Parse
method is that in case the input is not in correct format, it does not throw any exception and rather it returns a boolean indicating whether the value has been successfully parsed or not.
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
add a comment |
Use Double.Parse
:
private static double InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Double.Parse(Console.ReadLine());
}
add a comment |
Use Double.Parse.
This is the code:
Console.WriteLine("Enter a number : ");
string numString = Console.ReadLine(); //string
double numDouble = double.Parse(numString); //double
If you want to convert the double to string then use. Let's assume doubleText as the double.
string stringText = doubleText.ToString();
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Double's TryParse
method:
public Double? StringToDouble(String input){
if(Double.TryParse(input, out Double d)) {
Console.WriteLine("The double value is {0}", d);
return d;
}
else{
Console.WriteLine("The input string was not in correct format");
}
return null;
}
The advantage of the TryParse
method over the Parse
method is that in case the input is not in correct format, it does not throw any exception and rather it returns a boolean indicating whether the value has been successfully parsed or not.
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
add a comment |
You can use Double's TryParse
method:
public Double? StringToDouble(String input){
if(Double.TryParse(input, out Double d)) {
Console.WriteLine("The double value is {0}", d);
return d;
}
else{
Console.WriteLine("The input string was not in correct format");
}
return null;
}
The advantage of the TryParse
method over the Parse
method is that in case the input is not in correct format, it does not throw any exception and rather it returns a boolean indicating whether the value has been successfully parsed or not.
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
add a comment |
You can use Double's TryParse
method:
public Double? StringToDouble(String input){
if(Double.TryParse(input, out Double d)) {
Console.WriteLine("The double value is {0}", d);
return d;
}
else{
Console.WriteLine("The input string was not in correct format");
}
return null;
}
The advantage of the TryParse
method over the Parse
method is that in case the input is not in correct format, it does not throw any exception and rather it returns a boolean indicating whether the value has been successfully parsed or not.
You can use Double's TryParse
method:
public Double? StringToDouble(String input){
if(Double.TryParse(input, out Double d)) {
Console.WriteLine("The double value is {0}", d);
return d;
}
else{
Console.WriteLine("The input string was not in correct format");
}
return null;
}
The advantage of the TryParse
method over the Parse
method is that in case the input is not in correct format, it does not throw any exception and rather it returns a boolean indicating whether the value has been successfully parsed or not.
edited Jan 3 at 10:15
answered Jan 3 at 10:09


TranscendentTranscendent
3,25431641
3,25431641
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
add a comment |
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
The OPs problem is not that the string cannot be parsed. Thus your solution won´t help him/her at all.
– HimBromBeere
Jan 3 at 10:12
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
@HimBromBeere: Although I don't really agree with you, I improved my answer.
– Transcendent
Jan 3 at 10:15
add a comment |
Use Double.Parse
:
private static double InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Double.Parse(Console.ReadLine());
}
add a comment |
Use Double.Parse
:
private static double InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Double.Parse(Console.ReadLine());
}
add a comment |
Use Double.Parse
:
private static double InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Double.Parse(Console.ReadLine());
}
Use Double.Parse
:
private static double InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Double.Parse(Console.ReadLine());
}
answered Jan 3 at 10:09
NickNick
2,3671215
2,3671215
add a comment |
add a comment |
Use Double.Parse.
This is the code:
Console.WriteLine("Enter a number : ");
string numString = Console.ReadLine(); //string
double numDouble = double.Parse(numString); //double
If you want to convert the double to string then use. Let's assume doubleText as the double.
string stringText = doubleText.ToString();
add a comment |
Use Double.Parse.
This is the code:
Console.WriteLine("Enter a number : ");
string numString = Console.ReadLine(); //string
double numDouble = double.Parse(numString); //double
If you want to convert the double to string then use. Let's assume doubleText as the double.
string stringText = doubleText.ToString();
add a comment |
Use Double.Parse.
This is the code:
Console.WriteLine("Enter a number : ");
string numString = Console.ReadLine(); //string
double numDouble = double.Parse(numString); //double
If you want to convert the double to string then use. Let's assume doubleText as the double.
string stringText = doubleText.ToString();
Use Double.Parse.
This is the code:
Console.WriteLine("Enter a number : ");
string numString = Console.ReadLine(); //string
double numDouble = double.Parse(numString); //double
If you want to convert the double to string then use. Let's assume doubleText as the double.
string stringText = doubleText.ToString();
edited Jan 3 at 15:02


mason
23.9k74783
23.9k74783
answered Jan 3 at 12:02
Roshan KumarRoshan Kumar
268
268
add a comment |
add a comment |
5
Your method should return
double
– Backs
Jan 3 at 10:08
1
What is problem? (other than: your method should return double).
– isaeid
Jan 3 at 10:12
1
Ohh lol, didn't see that -_- thanks!
– Rammah
Jan 3 at 10:12
Remember to catch exceptions like
FormatException
for example.– Valentin P
Jan 3 at 10:14
@ValentinP Which is a really bad appraoch to prevent whrongly formatted input from being parsed. Better would be using
TryParse
. Don´t catch exceptions when you can avoid them in the first place.– HimBromBeere
Jan 3 at 10:15