0
        Console.WriteLine("Amount Spent (0.00):");
        float amount;
        string amountString;
        bool repeat = false;
        do
        {
            repeat = false;
            amountString = Console.ReadLine();
            try
            {
                amount = float.Parse(amountString);
            }
            catch (FormatException)
            {
                Console.WriteLine("Numeric Value Required.");
                repeat = true;
            }
            catch (OverflowException)
            {
                Console.WriteLine("Number too large/small for float.");
                repeat = true;
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid Input.");
                repeat = true;
            }
        } 
        while (repeat);
        return float.Parse(amountString); 

This Code works fine for regular numbers, however if I start entering ridiculously big numbers I get a returned value of 8. I would of expected the OverFlowException to catch a number too large. Shouldn't it catch them?

If I type in:

50000000000000000000000000000000000000

I get values such as 5E+37 which is think is to do with precision, but i expect this. But with one digit longer it just returns 8. i assume this is because it is too large but i would of thought the OverflowException in the tryCatch should catch it shouldn't it?

juharr
  • 31,741
  • 4
  • 58
  • 93
  • 4
    Are you asking about parsing an int or a float? Your title says one, the code says the other. – Retired Ninja May 26 '20 at 18:25
  • If you are working with float, welcome to the uncertainty of floating point math: https://www.youtube.com/watch?v=PZRI1IfStY0 – Christopher May 26 '20 at 18:34
  • I put your code in https://dotnetfiddle.net/ and 500000000000000000000000000000000000000 throws the OverflowException. Is this your full code? Also why are you parsing it again instead of returning 'amount'? – shox May 26 '20 at 18:37
  • Which .NET version are you using so far? Depending on it you may get exception (version before .NET Core 3.x and .NET Fw) or infinity value (.NET Core 3.x) – Pavel Anikhouski May 26 '20 at 19:03

1 Answers1

-1

Well first off, there is a discrepancy between what you have in code (float.Parse) and your question (Int.Parse)

Either event, you should probably be using a decimal instead of a float here.

Pay special attention to the difference between a binary floating point type, and a decimal floating point type. Decimal data type is likely what you're looking for here, as float doesn't have enough significant digits to store the number you're trying to parse accurately.

Float has 20 digits for the "whole number" part and then 3 for the fractional part (when you max it out with >=24 digits).

HaroldP1
  • 31
  • 7
  • 1
    [How do you figure `Float` has 20 and 3 digits?](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) How does this explain why the code is producing 8? – Eric Postpischil May 26 '20 at 21:04
  • This doesn't answer the question. This should be a comment not an answer. – shox May 26 '20 at 21:48