Home > database >  Frequency converter in C#
Frequency converter in C#

Time:11-02

So my program should in the end look like this in the console.

enter image description here

You basically just put in a random value of kilohertz (in the example its 50) And the program converts it into every hertz-"type". After that the program should convert it into period durations as you should see in the example picture. (Its in german, dw).

I just started coding in C#, its literally the second program im writing. (Task in the CS class).

This is my code so far. The program calculates it already correctly but i want the decimal points the same as it is in the example picture above. How do i do that? I tried google and some other stuff but I dont find something that fits into my task. (I read something about converting it back To.String but it doesnt work).

using System;
using System.Threading.Channels;

namespace Frequenzumwandler
{
    class program
    {

        public static void Main(string[] args)
        {

            //Introduce the program

            Console.WriteLine("\t*************************************************");
            Console.WriteLine("\t*            Berechnung von f und T             *");
            Console.WriteLine("\t*************************************************");

            //variables
            string input;
            double Hz, Mhz, Khz, Ghz, s, ms, us, ns;



            //input


            Console.Write("\n\tGeben Sie die Frequenz in kHz ein: ");
            input = Console.ReadLine();
            Khz = Convert.ToDouble(input);


            Console.Clear();


            //Calculation Hertz

            Hz = Khz * 1000;
            Mhz = Khz / 1000;
            Ghz = Khz / 1000000;

            //Calculation period duration

            s = 0.001 / Khz;
            ms = 1 / Khz;
            us = 1000 / Khz;
            ns = 1000000 / Khz;




            //Output Frequency table

            Console.WriteLine($"\n\tFrequenz in Hz:\t\t\t\t\t{Hz}\tHz");
            Console.WriteLine($"\tFrequenz in Khz:\t\t\t\t{Khz}\tKhz");
            Console.WriteLine($"\tFrequenz in Mhz:\t\t\t\t{Mhz}\tMhz");
            Console.WriteLine($"\tFrequenz in Ghz:\t\t\t\t{Ghz}\tGhz");

            //Output period duration table

            Console.WriteLine($"\n\tPeriodendauer in s:\t\t\t\t{s}\ts");
            Console.WriteLine($"\tPeriodendauer in ms:\t\t\t\t{ms}\tms");
            Console.WriteLine($"\tPeriodendauer in us:\t\t\t\t{us}\tus");
            Console.WriteLine($"\tPeriodendauer in ns:\t\t\t\t{ns}\tns");

            



        }

    }

}

Thanks!

Tried to make the output look like in the example

CodePudding user response:

You can specify the amount of decimal places you want to be displayed. As you already noticed, you can use the .ToString() method. In fact, the .ToString() method is always called, when you are using a value inside a string. To specify the decimal places you can use value.ToString("N6"), which always displays 6 decimal places. Your code then look like this:

Console.WriteLine($"\n\tPeriodendauer in s:\t\t\t\t{s.ToString("N6")}\ts");
Console.WriteLine($"\tPeriodendauer in ms:\t\t\t\t{ms.ToString("N6")}\tms");
Console.WriteLine($"\tPeriodendauer in us:\t\t\t\t{us.ToString("N6")}\tus");
Console.WriteLine($"\tPeriodendauer in ns:\t\t\t\t{ns.ToString("N6")}\tns");

C# provides an even more convenient way that actually does exactly the same as the above example, but it looks like this:

Console.WriteLine($"\n\tPeriodendauer in s:\t\t\t\t{s:N6}\ts");
Console.WriteLine($"\tPeriodendauer in ms:\t\t\t\t{ms:N6)}\tms");
Console.WriteLine($"\tPeriodendauer in us:\t\t\t\t{us:N6}\tus");
Console.WriteLine($"\tPeriodendauer in ns:\t\t\t\t{ns:N6}\tns");
  • Related