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;
}







-2
















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.










share|improve this question















marked as duplicate by Uwe Keim, Kirk Larkin, HimBromBeere, mason c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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




















-2
















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.










share|improve this question















marked as duplicate by Uwe Keim, Kirk Larkin, HimBromBeere, mason c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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
















-2












-2








-2









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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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
















  • 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










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














3 Answers
3






active

oldest

votes


















1














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.






share|improve this answer


























  • 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



















0














Use Double.Parse:



private static double InputDouble(double prompt)
{
Console.WriteLine("{0:s}: ", prompt);
return Double.Parse(Console.ReadLine());
}





share|improve this answer































    0














    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();





    share|improve this answer
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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.






      share|improve this answer


























      • 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
















      1














      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.






      share|improve this answer


























      • 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














      1












      1








      1







      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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



















      • 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













      0














      Use Double.Parse:



      private static double InputDouble(double prompt)
      {
      Console.WriteLine("{0:s}: ", prompt);
      return Double.Parse(Console.ReadLine());
      }





      share|improve this answer




























        0














        Use Double.Parse:



        private static double InputDouble(double prompt)
        {
        Console.WriteLine("{0:s}: ", prompt);
        return Double.Parse(Console.ReadLine());
        }





        share|improve this answer


























          0












          0








          0







          Use Double.Parse:



          private static double InputDouble(double prompt)
          {
          Console.WriteLine("{0:s}: ", prompt);
          return Double.Parse(Console.ReadLine());
          }





          share|improve this answer













          Use Double.Parse:



          private static double InputDouble(double prompt)
          {
          Console.WriteLine("{0:s}: ", prompt);
          return Double.Parse(Console.ReadLine());
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 10:09









          NickNick

          2,3671215




          2,3671215























              0














              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();





              share|improve this answer






























                0














                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();





                share|improve this answer




























                  0












                  0








                  0







                  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();





                  share|improve this answer















                  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();






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 15:02









                  mason

                  23.9k74783




                  23.9k74783










                  answered Jan 3 at 12:02









                  Roshan KumarRoshan Kumar

                  268




                  268















                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      Npm cannot find a required file even through it is in the searched directory

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith