NumericInput 1.0.1

dotnet add package NumericInput --version 1.0.1
NuGet\Install-Package NumericInput -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NumericInput" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NumericInput --version 1.0.1
#r "nuget: NumericInput, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NumericInput as a Cake Addin
#addin nuget:?package=NumericInput&version=1.0.1

// Install NumericInput as a Cake Tool
#tool nuget:?package=NumericInput&version=1.0.1

NumericInput

NumericInput is a library that simplifies the task of receiving numeric input and input validation in C#. When using Console.ReadLine() to receive numeric input, you need to convert the string to a number. This library streamlines that process.

Installation

To use the library, include using NumericInput; in your code.

Features

  • ReadInteger(): Prompts the user for an integer input and repeatedly requests input until a valid integer is entered.
  • ReadDouble(): Prompts the user for a double input and repeatedly requests input until a valid double is entered.
  • IsVaildNum(string value): Checks if the given string is a valid number.

Usage Examples

Traditional Input Method

class Program
{
    static void Main(string[] args)
    {
        // Read an integer
        Console.Write("Enter an integer: ");
        string intInput = Console.ReadLine();
        int intValue;
        while (!int.TryParse(intInput, out intValue))
        {
            Console.WriteLine("Invalid input. Please enter an integer.");
            Console.Write("Enter an integer: ");
            intInput = Console.ReadLine();
        }
        Console.WriteLine($"You entered: {intValue}");

        // Read a double
        Console.Write("Enter a decimal number: ");
        string doubleInput = Console.ReadLine();
        double doubleValue;
        while (!double.TryParse(doubleInput, out doubleValue))
        {
            Console.WriteLine("Invalid input. Please enter a number.");
            Console.Write("Enter a decimal number: ");
            doubleInput = Console.ReadLine();
        }
        Console.WriteLine($"You entered: {doubleValue}");

        // Use IsValidNum
        Console.Write("Enter a number or text: ");
        string input = Console.ReadLine();
        double value;
        if (double.TryParse(input, out value))
        {
            Console.WriteLine($"You entered a valid number: {value}");
        }
        else
        {
            Console.WriteLine($"'{input}' is not a valid number.");
        }
    }
}

Using NumericInput Library

using NumericInput;

class Program
{
    static void Main(string[] args)
    {
        // Read an integer
        Console.Write("Enter an integer: ");
        int intValue = Numeric.ReadInteger();
        Console.WriteLine($"You entered: {intValue}");

        // Read a double
        Console.Write("Enter a decimal number: ");
        double doubleValue = Numeric.ReadDouble();
        Console.WriteLine($"You entered: {doubleValue}");

        // Use IsValidNum
        Console.Write("Enter a number or text: ");
        string input = Numeric.ReadInput();

        if (Numeric.IsValidNum(input))
        {
            double value = double.Parse(input);
            Console.WriteLine($"You entered a valid number: {value}");
        }
        else
        {
            Console.WriteLine($"'{input}' is not a valid number.");
        }
    }
}

Contributing

If you'd like to contribute to this project, please open an Issue or a Pull Request. All contributions are welcome!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 64 5/17/2024
1.0.0 56 5/17/2024