Boost Your .NET Projects: Unleashing the Power of Spargine’s Numeric Extension Methods

Spargine is a set of open-source assemblies and NuGet packages for .NET 8, developed and maintained by me since the release of .NET 2. These assemblies are integral to my projects and are currently in production at my company. You can access the source code and NuGet packages through the following links:

This documentation details the NumericExtensions class within the DotNetTips.Spargine.8.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.
  • 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.
  • 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.
  • 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.

Summary

I am confident that these enhanced methods in Spargine will greatly benefit your projects by improving performance and reliability. Detailed benchmark results are available on GitHub. The success of open-source projects like Spargine depends significantly on community involvement. If you find these updates useful or have ideas for further improvements, I encourage you to contribute. Whether by submitting a pull request, reporting issues, or suggesting new features, your input is invaluable.

Together, we can continue to make Spargine a powerful and essential tool for the .NET community. Your feedback and suggestions are highly appreciated, so please share them in the comments section.

If you are interested in contributing to this project or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!

Thank you for your support, and happy coding!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

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 preproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.