C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: There are 6 methods that receive doubles and return doubles in the ConvertTimeUnit class. Place the ConvertTimeUnit class in a file.
StaticUsing alias: This means you can type C instead of ConvertTimeUnits. This may make the class easier to read (or possibly not).
Using AliasC# program that converts time units
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;
}
}
Output
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
Note: Thanks to Rene Schuchter for pointing out this error and providing corrections.
Important: With the current code, I have tested the results against the calculator on DuckDuckGo.