TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Convert Nanoseconds, Microseconds, Milliseconds

This C# example program converts figures in nanoseconds, microseconds and milliseconds.

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.

Google search 1Google search 2

Summary. We converted milliseconds, microseconds and nanoseconds to each other programmatically using the C# language. We also tested the methods to make sure they work. I ran into several typos when testing the methods.

Finally: It would be easy to translate these methods to other languages. We could implement this code in Perl, Python or Ruby.

Python Tutorial: Dot Net Perls


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf