Convert nanoseconds. With C# code, figures in milliseconds, microseconds and nanoseconds can be converted. We want to convert those figures to one of the other two units.
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).
An example. These methods are trivial to understand, but this code prevents us from having to repeat these conversions. They are placed in a static class.
Info There are 6 methods that receive doubles and return doubles in the ConvertTimeUnit class. Place the ConvertTimeUnit class in a file.
using System;
using C = ConvertTimeUnits;
class Program
{
static void Main()
{
// Test all the methods.// ... Be sure to validate these results.
double nanoseconds1 = C.ConvertMillisecondsToNanoseconds(1000);
Console.WriteLine("1000 milliseconds to nanoseconds: {0}", nanoseconds1);
double nanoseconds2 = C.ConvertMicrosecondsToNanoseconds(1000);
Console.WriteLine("1000 microseconds to nanoseconds: {0}", nanoseconds2);
double microseconds1 = C.ConvertMillisecondsToMicroseconds(1000);
Console.WriteLine("1000 milliseconds to microseconds: {0}", microseconds1);
double microseconds2 = C.ConvertNanosecondsToMicroseconds(1000);
Console.WriteLine("1000 nanoseconds to microseconds: {0}", microseconds2);
double milliseconds1 = C.ConvertMicrosecondsToMilliseconds(1000);
Console.WriteLine("1000 microseconds to milliseconds: {0}", milliseconds1);
double milliseconds2 = C.ConvertNanosecondsToMilliseconds(1000);
Console.WriteLine("1000 nanoseconds to milliseconds: {0}", milliseconds2);
}
}
public static class ConvertTimeUnits
{
public static double ConvertMillisecondsToNanoseconds(double milliseconds)
{
return milliseconds * 1000000;
}
public static double ConvertMicrosecondsToNanoseconds(double microseconds)
{
return microseconds * 1000;
}
public static double ConvertMillisecondsToMicroseconds(double milliseconds)
{
return milliseconds * 1000;
}
public static double ConvertNanosecondsToMilliseconds(double nanoseconds)
{
return nanoseconds * 0.000001;
}
public static double ConvertMicrosecondsToMilliseconds(double microseconds)
{
return microseconds * 0.001;
}
public static double ConvertNanosecondsToMicroseconds(double nanoseconds)
{
return nanoseconds * 0.001;
}
}1000 milliseconds to nanoseconds: 1000000000
1000 microseconds to nanoseconds: 1000000
1000 milliseconds to microseconds: 1000000
1000 nanoseconds to microseconds: 1
1000 microseconds to milliseconds: 1
1000 nanoseconds to milliseconds: 0.001
A summary. We converted milliseconds, microseconds and nanoseconds with C# code. We tested the methods. Extensive testing is necessary for important work.
It would be easy to translate these methods to other languages. Once tested, the methods can be ported alongside some required input and output values to ensure correctness.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).