C# Convert Nanoseconds, Microseconds, Milliseconds

Convert figures in nanoseconds, microseconds and milliseconds.

dot net perls

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.Casts

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 alias: This means you can type C instead of ConvertTimeUnits. This may make the class easier to read (or possibly not).
Using Alias
C# 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

An important note. I published this code without enough testing. Two of the methods were left unimplemented, so gave the wrong result.
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.

A summary. We converted milliseconds, microseconds and nanoseconds programmatically. 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.


© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.