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(double a, double b)
Calculates the average of two numbers. This method supports the following types:decimal,double,int, andlong. - BytesToMegabytes(long bytes)
Converts a value representing bytes into megabytes, facilitating easier interpretation of large data sizes. - Decrement(int value, int lowerBound = 0, int step = 1)
Decreases the specified integer by a given step, ensuring it does not go below a defined lower bound. - EnsureMinimum(int value, int minValue)
Ensures that the given value is not less than the specified minimum value. - FormatSize(long fileSize)
Converts a file size from bytes into a human-readable string format (e.g., KB, MB, GB, TB). This method supportsdoubleandlongtypes. - FormatTime(this double milliseconds)
Formats the time as a human-readable string (e.g., “5 hours 10 minutes 20 seconds” or “20 milliseconds” for values less than 1000). This method is overloaded to uselong. - Increment(int value, int upperBound = 100, int step = 1)
Increases the specified integer by a given step, ensuring it does not exceed a defined upper bound. - IsEven(decimal value)
Checks if a given number is even. It supports multiple types, includingdecimal,double,float,int,long,sbyte, andshort. - IsInRange(int value, int lower, int upper)
Determines whether a value falls within a specified range. This method works withdecimal,double,int, andlongtypes. - IsInRangeThrowsException(double value, double lower, double upper)
Similar toIsInRange()but throws anOutOfRangeExceptionif the value is outside the defined range. This method also supportsdecimal,double,int, andlong. - IsInterval(int value, int interval)
Checks whether a specified integer is divisible by a given interval. - IsIntervalThrowsException(int value, int interval, string paramName)
Similar to IsInterval(), but throws anArgumentOutOfRangeExceptionif the integer is not divisible by the given interval. - IsNegative(double value)
Determines if a given value is negative. This method supportsdecimal,double,float,int,long,sbyte, andshort. - MillisecondsToString(long totalMilliseconds)
Converts a duration in milliseconds into a formatted string. This method works withint,long, andTimeSpan. Example output: “27:46:39”. - RoundToPowerOf2(int value)
Rounds a given integer up to the nearest power of two. - ToFormattedString(int input, NumericFormat format)
Converts a numeric value to a formatted string based on the specified format. Supported types includedouble,int,long,short,uint,ulong, andushort. 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(int value)
Converts negative values to 0, while leaving positive values unchanged. This method supportsdecimal,int, andlong. - ToRomanNumeral(int number)
Converts an integer to its equivalent Roman numeral representation. - ToStringOrEmpty(int value, int lowerLimit = 100, int upperLimit = 9000, string defaultText)
Returns the string representation of an integer if it falls outside a specified range; otherwise, it returns a default text. - ToWords(int value)
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.

