Table of Contents

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

n double

The positive integer to calculate a factorial number by.

Returns

double

The factorial number calculated from n, or PositiveInfinity if n is to high a value.

Exceptions

ArgumentOutOfRangeException

n is 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

input double

The double to extend.

Returns

DateTime

A DateTime that is equivalent to input.

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

value double

A double-precision floating-point number to be rounded.

accuracy RoundOffAccuracy

The accuracy to use in the rounding.

Returns

double

The integer value closest to the specified accuracy of value.
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

value double

The value to be converted.

timeUnit TimeUnit

One of the enumeration values that specifies the outcome of the conversion.

Returns

TimeSpan

A TimeSpan that corresponds to value from timeUnit.

Exceptions

OverflowException

The value paired with timeUnit is outside its valid range.

ArgumentOutOfRangeException

timeUnit was outside its valid range.