Class DoubleExtensions
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Extension methods for the double struct.
public static class DoubleExtensions
- Inheritance
-
DoubleExtensions
Examples
DoubleExtensions provides extension methods for Double covering Unix epoch conversion, time span creation, factorial computation, and precision rounding. This example starts with a Unix timestamp of 1617738277 and converts it to a local DateTime via FromUnixEpochTime, creates a TimeSpan from 3661 seconds using ToTimeSpan, computes 5! using Factorial, and rounds 123456789.987654321 to the nearest thousand and million using RoundOff. Console output shows the resulting date (O format), the duration (01:01:01), the factorial value (120), and the rounded figures (123457000 and 123000000).
using System;
using Cuemon;
using Cuemon.Extensions;
namespace MyApp.Numerics;
public static class DoubleExtensionsExample
{
public static void Demonstrate()
{
double unixTimestamp = 1617738277d;
DateTime fromUnix = unixTimestamp.FromUnixEpochTime().ToLocalTime();
TimeSpan fromSeconds = 3661d.ToTimeSpan(TimeUnit.Seconds);
double factorial = 5d.Factorial();
double value = 123456789.987654321d;
double nearestThousand = value.RoundOff(RoundOffAccuracy.NearestThousandth);
double nearestMillion = value.RoundOff(RoundOffAccuracy.NearestMillion);
Console.WriteLine(fromUnix.ToString("O"));
Console.WriteLine(fromSeconds);
Console.WriteLine(factorial);
Console.WriteLine(nearestThousand);
Console.WriteLine(nearestMillion);
}
}
Methods
Factorial(double)
Calculates the factorial of a positive integer n denoted by n!.
public static double Factorial(this double n)
Parameters
ndoubleThe positive integer to calculate a factorial number by.
Returns
- double
The factorial number calculated from
n, or PositiveInfinity ifnis to high a value.
Exceptions
- ArgumentOutOfRangeException
nis lower than 0.
FromUnixEpochTime(double)
Converts the specified input of an UNIX Epoch time to its equivalent DateTime structure.
public static DateTime FromUnixEpochTime(this double input)
Parameters
Returns
RoundOff(double, RoundOffAccuracy)
Rounds a double-precision floating-point value to the nearest integral value closest to the specified accuracy.
public static double RoundOff(this double value, RoundOffAccuracy accuracy)
Parameters
valuedoubleA double-precision floating-point number to be rounded.
accuracyRoundOffAccuracyThe accuracy to use in the rounding.
Returns
- double
The integer value closest to the specified
accuracyofvalue.
Note that this method returns a double instead of an integral type.
ToTimeSpan(double, TimeUnit)
Converts the specified value to its equivalent TimeSpan representation.
public static TimeSpan ToTimeSpan(this double value, TimeUnit timeUnit)
Parameters
valuedoubleThe value to be converted.
timeUnitTimeUnitOne of the enumeration values that specifies the outcome of the conversion.
Returns
Exceptions
- OverflowException
The
valuepaired withtimeUnitis outside its valid range.- ArgumentOutOfRangeException
timeUnitwas outside its valid range.