C#: .NET: Cast

Convert nanoseconds. Figures in milliseconds,
microseconds
and nanoseconds can be converted. We want to convert those figures to one of the other two units. This is useful for benchmarking. It helps also with certain scientific calculations.

Example. First, milliseconds are equal to 1000 microseconds each, and microseconds are equal to 1000 nanoseconds each. In the same scale, milliseconds are equal to one million nanoseconds (1000 * 1000).

Note:The following methods are trivial to understand, but this code prevent us from having to write these conversions repeatedly.

C# program that converts time units

using System;
using C = ConvertTimeUnits;

class Program
{
static void Main()
{
//
// To nanoseconds.
//
double nanoseconds1 = C.ConvertMillisecondsToNanoseconds(1000);
Console.WriteLine(nanoseconds1);

double nanoseconds2 = C.ConvertMicrosecondsToNanoseconds(1000);
Console.WriteLine(nanoseconds2);

//
// To microseconds.
//
double microseconds1 = C.ConvertMillisecondsToMicroseconds(1000);
Console.WriteLine(microseconds1);

double microseconds2 = C.ConvertNanosecondsToMicroseconds(1000);
Console.WriteLine(microseconds2);

//
// To milliseconds.
//
double milliseconds1 = C.ConvertMicrosecondsToMilliseconds(1000);
Console.WriteLine(milliseconds1);

double milliseconds2 = C.ConvertNanosecondsToMilliseconds(1000);
Console.WriteLine(milliseconds2);
}
}

public static class ConvertTimeUnits
{
public static double ConvertMillisecondsToNanoseconds(double milliseconds)
{
// One million nanoseconds in one nanosecond.
return milliseconds * 1000000;
}
public static double ConvertMicrosecondsToNanoseconds(double microseconds)
{
// One thousand microseconds in one nanosecond.
return microseconds * 0.001;
}

public static double ConvertMillisecondsToMicroseconds(double milliseconds)
{
// One thousand milliseconds in one microsecond.
return milliseconds * 1000;
}

public static double ConvertNanosecondsToMilliseconds(double nanoseconds)
{
// One million nanoseconds in one millisecond.
return nanoseconds * 0.000001;
}

public static double ConvertMicrosecondsToMilliseconds(double microseconds)
{
// One thousand microseconds in one millisecond.
return microseconds * 1000;
}

public static double ConvertNanosecondsToMicroseconds(double nanoseconds)
{
// One thousand nanoseconds in one microsecond.
return nanoseconds * 1000;
}
}

Output

1000000000
1
1000000
1000000
1000000
0.001

There are six conversion methods that receive doubles and return doubles in the ConvertTimeUnit static class. You will want to put the ConvertTimeUnit class in a file. The methods are all static—they do not save state.

Static

And:Near the top of the program, you will see a using alias statement. This means you can type C instead of ConvertTimeUnits.

Note:This is not critical for the example to work, but in this case may lead to easier understanding and review.

Using Alias Example

Google. With unit conversion methods in any artificial language, it is useful to check Google for accuracy. For example, the above code converts 1000 microseconds to milliseconds and it results in one millisecond.

Also:It tries to convert 1000 nanoseconds to milliseconds at the end. The valid returned is 0.001, which is validated at Google.