Home
C#
Convert Nanoseconds, Microseconds, Milliseconds
Updated Nov 17, 2022
Dot Net Perls
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).
Convert Milliseconds
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.
static
Detail This means you can type C instead of ConvertTimeUnits. This may make the class easier to read (or possibly not).
using Alias
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 17, 2022 (simplify).
Home
Changes
© 2007-2025 Sam Allen