none
C# Double parse

    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

  • It's because NaN (aka Not a Number) is in fact a value of Double Type.  Try literally any other string that is both not numeric and not NaN.

                var mScore = "blargh";
                double fScore;
                bool isValidDouble = double.TryParse(mScore, out fScore);
                
                double myNaN = double.NaN;


    It never hurts to try. In the worst-case scenario, you'll learn something.


    • Edited by Andrew B. Painter Friday, December 7, 2018 5:32 AM
    • Marked as answer by StSingh Wednesday, December 12, 2018 11:14 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

  • It's because NaN (aka Not a Number) is in fact a value of Double Type.  Try literally any other string that is both not numeric and not NaN.

                var mScore = "blargh";
                double fScore;
                bool isValidDouble = double.TryParse(mScore, out fScore);
                
                double myNaN = double.NaN;


    It never hurts to try. In the worst-case scenario, you'll learn something.


    • Edited by Andrew B. Painter Friday, December 7, 2018 5:32 AM
    • Marked as answer by StSingh Wednesday, December 12, 2018 11:14 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
  • 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.

    It's a little bit anti-intuitive, but in computing numbers aren't really numbers so much as positive electrical charges on a particular set of conductive pins at any given time.  It's why zero (the total absence of charge) is a physical quantity and NaN is a valid double/float value.

    In computing the sound of one hand clapping is the smell of hot metal.


    It never hurts to try. In the worst-case scenario, you'll learn something.


    Friday, December 7, 2018 12:14 AM
  • 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
  • You'll want to take that up with the Institute of Electrical and Electronics Engineers, who standardized the NaN value in double precision floating point formats in their document IEEE754.  If a high-level language ignored this specification, it would not be a programming language.  In fact, since this standardized NaN value is output by CPU hardware on certain operations, it wouldn't even be useful as a meta language for describing double/float operations.

    It honestly usually hurts to try.  The pain reminds you that you're alive.  Continuing in spite of it proves you aren't worthless.


    It never hurts to try. In the worst-case scenario, you'll learn something.

    Friday, December 7, 2018 2:34 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
  • I'll see your animated buffoon and raise you a geriatric muppet:  "Do or do not do!  THERE IS NO TRY!"

    It never hurts to try. In the worst-case scenario, you'll learn something.

    Friday, December 7, 2018 3:50 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