Skip to content

Commit

Permalink
240117
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed Jan 17, 2024
1 parent 3909ea6 commit 92aa325
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/YANLib/Core/YANNum.Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ namespace YANLib.Core;

public static partial class YANNum
{
/// <summary>
/// Converts the specified object to a decimal value.
/// If the conversion fails, attempts to use the provided default value for conversion.
/// If both conversions fail, returns the default value of a decimal.
/// </summary>
/// <param name="val">The object to be converted to a decimal. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>The decimal value of the converted object, or the converted default value, or the default value of a decimal if both conversions fail.</returns>
public static decimal ToDecimal(this object? val, object? dfltVal = null)
{
try
Expand All @@ -16,13 +24,53 @@ public static decimal ToDecimal(this object? val, object? dfltVal = null)
}
}

/// <summary>
/// Converts a collection of objects to their respective decimal values.
/// If the collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the collection is converted to a decimal value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The collection of objects to be converted to decimals. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of decimals representing the converted values, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<decimal>? ToDecimals(this IEnumerable<object?>? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDecimal(dfltVal));

/// <summary>
/// Converts a collection (ICollection) of objects to their respective decimal values.
/// If the collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the collection is converted to a decimal value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The ICollection of objects to be converted to decimals. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of decimals representing the converted values, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<decimal>? ToDecimals(this ICollection<object?>? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDecimal(dfltVal));

/// <summary>
/// Converts an array of objects to their respective decimal values.
/// If the array is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the array is converted to a decimal value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The array of objects to be converted to decimals. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An array of decimals representing the converted values, or <see langword="null"/> if the input array is <see langword="null"/> or empty.</returns>
public static IEnumerable<decimal>? ToDecimals(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDecimal(dfltVal));

/// <summary>
/// Generates a random decimal value within the specified minimum and maximum bounds.
/// If the minimum or maximum values are invalid or unspecified, the full range of decimal values is considered.
/// </summary>
/// <param name="min">The minimum bound for the decimal value. Can be <see langword="null"/>.</param>
/// <param name="max">The maximum bound for the decimal value. Can be <see langword="null"/>.</param>
/// <returns>A randomly generated decimal value within the specified bounds.</returns>
public static decimal GenerateRandomDecimal(object? min = null, object? max = null) => new Random().NextDecimal(min, max);

/// <summary>
/// Generates a collection of random decimal values within specified minimum and maximum bounds, and of a specified size.
/// If the minimum or maximum values are invalid or unspecified, uses the full range of decimal values.
/// If the size is unspecified, defaults to a collection of size 0.
/// </summary>
/// <param name="min">The minimum bound for the decimal values. Can be <see langword="null"/>.</param>
/// <param name="max">The maximum bound for the decimal values. Can be <see langword="null"/>.</param>
/// <param name="size">The number of random decimal values to generate. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of randomly generated decimal values within the specified bounds and of the specified size.</returns>
public static IEnumerable<decimal> GenerateRandomDecimals(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDecimal(min, max));
}
48 changes: 48 additions & 0 deletions lib/YANLib/Core/YANNum.Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ namespace YANLib.Core;

public static partial class YANNum
{
/// <summary>
/// Converts the specified object to a double value.
/// If the conversion fails, attempts to use the provided default value for conversion.
/// If both conversions fail, returns the default value of a double.
/// </summary>
/// <param name="val">The object to be converted to a double. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>The double value of the converted object, or the converted default value, or the default value of a double if both conversions fail.</returns>
public static double ToDouble(this object? val, object? dfltVal = null)
{
try
Expand All @@ -16,13 +24,53 @@ public static double ToDouble(this object? val, object? dfltVal = null)
}
}

/// <summary>
/// Converts a collection of objects to their respective double values.
/// If the collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the collection is converted to a double value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The collection of objects to be converted to doubles. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of doubles representing the converted values, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<double>? ToDoubles(this IEnumerable<object?>? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDouble(dfltVal));

/// <summary>
/// Converts a collection (ICollection) of objects to their respective double values.
/// If the collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the collection is converted to a double value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The ICollection of objects to be converted to doubles. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of doubles representing the converted values, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<double>? ToDoubles(this ICollection<object?>? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDouble(dfltVal));

/// <summary>
/// Converts an array of objects to their respective double values.
/// If the array is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the array is converted to a double value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The array of objects to be converted to doubles. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An array of doubles representing the converted values, or <see langword="null"/> if the input array is <see langword="null"/> or empty.</returns>
public static IEnumerable<double>? ToDoubles(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDouble(dfltVal));

/// <summary>
/// Generates a random double value within the specified minimum and maximum bounds.
/// If the minimum or maximum values are invalid or unspecified, the full range of double values is considered.
/// </summary>
/// <param name="min">The minimum bound for the double value. Can be <see langword="null"/>.</param>
/// <param name="max">The maximum bound for the double value. Can be <see langword="null"/>.</param>
/// <returns>A randomly generated double value within the specified bounds.</returns>
public static double GenerateRandomDouble(object? min = null, object? max = null) => new Random().NextDouble(min, max);

/// <summary>
/// Generates a collection of random double values within specified minimum and maximum bounds, and of a specified size.
/// If the minimum or maximum values are invalid or unspecified, uses the full range of double values.
/// If the size is unspecified, defaults to a collection of size 0.
/// </summary>
/// <param name="min">The minimum bound for the double values. Can be <see langword="null"/>.</param>
/// <param name="max">The maximum bound for the double values. Can be <see langword="null"/>.</param>
/// <param name="size">The number of random double values to generate. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of randomly generated double values within the specified bounds and of the specified size.</returns>
public static IEnumerable<double> GenerateRandomDoubles(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDouble(min, max));
}
48 changes: 48 additions & 0 deletions lib/YANLib/Core/YANNum.Float.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ namespace YANLib.Core;

public static partial class YANNum
{
/// <summary>
/// Converts the specified object to a float value.
/// If the conversion fails, attempts to use the provided default value for conversion.
/// If both conversions fail, returns the default value of a float.
/// </summary>
/// <param name="val">The object to be converted to a float. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>The float value of the converted object, or the converted default value, or the default value of a float if both conversions fail.</returns>
public static float ToFloat(this object? val, object? dfltVal = null)
{
try
Expand All @@ -16,13 +24,53 @@ public static float ToFloat(this object? val, object? dfltVal = null)
}
}

/// <summary>
/// Converts a collection of objects to their respective float values.
/// If the collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the collection is converted to a float value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The collection of objects to be converted to floats. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of floats representing the converted values, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<float>? ToFloats(this IEnumerable<object?>? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToFloat(dfltVal));

/// <summary>
/// Converts a collection (ICollection) of objects to their respective float values.
/// If the collection is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the collection is converted to a float value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The ICollection of objects to be converted to floats. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of floats representing the converted values, or <see langword="null"/> if the input collection is <see langword="null"/> or empty.</returns>
public static IEnumerable<float>? ToFloats(this ICollection<object?>? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToFloat(dfltVal));

/// <summary>
/// Converts an array of objects to their respective float values.
/// If the array is <see langword="null"/> or empty, returns <see langword="null"/>.
/// Each object in the array is converted to a float value; if conversion fails, uses the provided default value.
/// </summary>
/// <param name="vals">The array of objects to be converted to floats. Can be <see langword="null"/>.</param>
/// <param name="dfltVal">The default value to use if conversion fails. Can be <see langword="null"/>.</param>
/// <returns>An array of floats representing the converted values, or <see langword="null"/> if the input array is <see langword="null"/> or empty.</returns>
public static IEnumerable<float>? ToFloats(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToFloat(dfltVal));

/// <summary>
/// Generates a random float value within the specified minimum and maximum bounds.
/// If the minimum or maximum values are invalid or unspecified, the full range of float values is considered.
/// </summary>
/// <param name="min">The minimum bound for the float value. Can be <see langword="null"/>.</param>
/// <param name="max">The maximum bound for the float value. Can be <see langword="null"/>.</param>
/// <returns>A randomly generated float value within the specified bounds.</returns>
public static float GenerateRandomFloat(object? min = null, object? max = null) => new Random().NextSingle(min, max);

/// <summary>
/// Generates a collection of random float values within specified minimum and maximum bounds, and of a specified size.
/// If the minimum or maximum values are invalid or unspecified, uses the full range of float values.
/// If the size is unspecified, defaults to a collection of size 0.
/// </summary>
/// <param name="min">The minimum bound for the float values. Can be <see langword="null"/>.</param>
/// <param name="max">The maximum bound for the float values. Can be <see langword="null"/>.</param>
/// <param name="size">The number of random float values to generate. Can be <see langword="null"/>.</param>
/// <returns>An enumerable collection of randomly generated float values within the specified bounds and of the specified size.</returns>
public static IEnumerable<float> GenerateRandomFloats(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomFloat(min, max));
}
Loading

0 comments on commit 92aa325

Please sign in to comment.