none
C# Double parse RRS feed

  • Question

  • hi,

    I want to check if my string is double or string... if its a string I want to replace it with 0

    var mScore = "NaN";

    If i parse this string it returns true for double.TryParse... which wierd as per my expectations...

    double Fscore;

    Bool isValidDouble = double.TryParse(mScore, out Fscore);

    So how i verify if my string is double or not?

    Thanks for the help.

    S

    Thursday, December 6, 2018 10:38 PM

Answers

  • Content Removed
    • Marked as answer by StSingh Wednesday, December 12, 2018 11:14 AM
    • Edited by Andrew B. Painter Friday, January 18, 2019 2:42 AM
    Thursday, December 6, 2018 11:38 PM
  • Isn't that ridiculous? TryParse is supposed to return false if the input is not a valid number, yet it returns true for the very value that's defined as meaning not a number.

    One simple way out of this dilemma might be to change the casing so the input is bound to be invalid.

    bool isValidDouble = double.TryParse(mScore.ToLower(), out fScore); // Use lower case, so it definitely won't be valid.

    • Marked as answer by StSingh Wednesday, December 12, 2018 11:14 AM
    Thursday, December 6, 2018 11:53 PM
  • Here are my results, not results.Equals vs == in the example below.

    var mScore = "NaN";
    Console.WriteLine(double.TryParse(mScore, out var result) ? $"Got: {result}" : "Not double");
    Console.WriteLine(!result.Equals(double.NaN) ? $"Result is '{result}'" : "NaN");
    Console.WriteLine(result == double.NaN);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 7, 2018 5:14 PM
    Moderator
  • In order to exclude more special strings, such as “Infinity” and “-Infinity” (depending on current language), try this too:

    bool isValidDouble = double.TryParse( mScore, out Fscore ) && !double.IsNaN( Fscore ) && !double.IsInfinity( Fscore );


    Friday, December 7, 2018 5:23 PM

All replies

  • Content Removed
    • Marked as answer by StSingh Wednesday, December 12, 2018 11:14 AM
    • Edited by Andrew B. Painter Friday, January 18, 2019 2:42 AM
    Thursday, December 6, 2018 11:38 PM
  • Isn't that ridiculous? TryParse is supposed to return false if the input is not a valid number, yet it returns true for the very value that's defined as meaning not a number.

    One simple way out of this dilemma might be to change the casing so the input is bound to be invalid.

    bool isValidDouble = double.TryParse(mScore.ToLower(), out fScore); // Use lower case, so it definitely won't be valid.

    • Marked as answer by StSingh Wednesday, December 12, 2018 11:14 AM
    Thursday, December 6, 2018 11:53 PM
  • True, but a high-level language is supposed to insulate the user from all the electrical engineering-type doovers under the hood. I still think it's ridiculous.

    Sometimes it hurts to try. But at least in the worst-case scenario, someone might learn something by examining your corpse.

    Friday, December 7, 2018 12:36 AM
  • "Trying is the first step on the road to failure."  -  Homer Simpson

    (I don't have anything to add to the debate. I just love that quote.)

    Friday, December 7, 2018 3:15 AM
  • Here are my results, not results.Equals vs == in the example below.

    var mScore = "NaN";
    Console.WriteLine(double.TryParse(mScore, out var result) ? $"Got: {result}" : "Not double");
    Console.WriteLine(!result.Equals(double.NaN) ? $"Result is '{result}'" : "NaN");
    Console.WriteLine(result == double.NaN);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 7, 2018 5:14 PM
    Moderator
  • In order to exclude more special strings, such as “Infinity” and “-Infinity” (depending on current language), try this too:

    bool isValidDouble = double.TryParse( mScore, out Fscore ) && !double.IsNaN( Fscore ) && !double.IsInfinity( Fscore );


    Friday, December 7, 2018 5:23 PM
  • Thanks everyone for chipping in and providing your valueable inputs.
    Wednesday, December 12, 2018 11:11 AM