Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
This documentation details the NumericExtensions class within the DotNetTips.Spargine.Extensions project, which provides extension methods for the number types.
Methods
The NumericExtensions class offers a wide range of utility methods designed to simplify handling numeric types in .NET. Below is an overview of the key methods provided:
- Average()
Calculates the average of two numbers. This method supports the following types: decimal, double, int, and long. - BytesToMegabytes()
Converts a value representing bytes into megabytes, facilitating easier interpretation of large data sizes. - Clamp(int minimum, int maximum)
Clamps an int to the inclusive range defined by minimum and maximum. - Decrement()
Decreases the specified integer by a given step, ensuring it does not go below a defined lower bound. - FormatSize()
Converts a file size from bytes into a human-readable string format (e.g., KB, MB, GB, TB). This method supports double and long types. - Increment()
Increases the specified integer by a given step, ensuring it does not exceed a defined upper bound. - IsEven()
Checks if a given number is even. It supports multiple types including decimal, double, float, int, long, sbyte, and short. - IsInRange()
Determines whether a value falls within a specified range. This method works with decimal, double, int, and long types. - IsInRangeThrowsException()
Similar to IsInRange() but throws an OutOfRangeException if the value is outside the defined range. This method also supports decimal, double, int, and long. - IsInterval()
Checks whether a specified integer is divisible by a given interval. - IsIntervalThrowsException()
Similar to IsInterval(), but throws an ArgumentOutOfRangeException if the integer is not divisible by the given interval. - IsNegative()
Determines if a given value is negative. This method supports decimal, double, float, int, long, sbyte, and short. - MillisecondsToString()
Converts a duration in milliseconds into a formatted string. This method works with int, long, and TimeSpan. Example output: “27:46:39”. - RoundToPowerOf2()
Rounds a given integer up to the nearest power of two. - RoundToNearestMultiple(int multiple)
Rounds an int to the nearest multiple of multiple using midpoint rounding away from zero. - ToFormattedString()
Converts a numeric value to a formatted string based on the specified format. Supported types include double, int, long, short, uint, ulong, and ushort. Example formats include currency, decimal, exponential, fixed-point, general, hexadecimal, number, and percentage.
Example Output:- Currency: “$2,103,162,670.00”
- Exponential: “2.103163E+009”
- Percent: “210,316,267,000.00%”
- ToPositiveValue()
Converts negative values to 0, while leaving positive values unchanged. This method supports decimal, int, and long. - ToRomanNumeral()
Converts an integer to its equivalent Roman numeral representation. - TryParseInvariant(this ReadOnlySpan<char> input, out int value)
Attempts to parse an int from a ReadOnlySpan using invariant culture. Overloaded to use double and decimal. - ToStringOrEmpty()
Returns the string representation of an integer if it falls outside a specified range; otherwise, it returns a default text. - ToWords()
Converts a given integer to its equivalent in English words.
Example:
Input: 54928
Output: “Fifty-Four Thousand Nine Hundred and Twenty-Eight”
These methods provide a wide array of operations, from basic numeric checks and conversions to more complex formatting and range validations, making numeric manipulation in .NET simpler and more efficient.
NumericFormat
NumericFormat makes it easy to use common formats for numbers. Much easier than looking them up every time you need to format a number.
- Binary
Formats an integer as a binary string. Example: 1111111111111111111111111111111. - Currency
Currency format specifier. Example: $2,147,483,647.00 - CurrencyWholeNumber
Currency format specifier with no decimal places. Example: $2,147,483,647 - Decimal
Decimal format specifier. Example: 21474836.47 - Exponential
Scientific (exponential). Example: 2.147484E+009 - FixedPoint
Fixed-point format specifier. Example: 2147483647.00 - FourDecimalPlaces
Number format specifier with four decimal places. Example: 2,147,483,647.0000 - General
General format specifier. Example: 2147483647 - Hexadecimal
Uppercase hexadecimal format specifier. Example: 7FFFFFFF - HexadecimalLower
Lowercase hexadecimal format specifier. Example: 7fffffff - Number
Number format specifier. Example: 2,147,483,647.00 - OneDecimalPlace
Number format specifier with one decimal place. Example: 2,147,483,647.0 - Percent
Percent format specifier. Example: 214,748,364,700.00% - PercentWholeNumber
Percent format specifier with no decimal places. Example: 214,748,364,700% - RoundTrip
Round-trip format specifier. Example: 8.988465674311579E+307 - TwoDecimalPlaces
Number format specifier with two decimal places. Example: 2,147,483,647.00 - WholeNumber
Number format specifier with no decimal places. Example: 2,147,483,647
Summary
I am confident that these methods in Spargine will greatly benefit your projects by improving performance and reliability. Detailed benchmark results are available on GitHub.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be reproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.


You must be logged in to post a comment.