0

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

xVanDaLL
  • 3
  • 2
  • See if this helps https://stackoverflow.com/questions/6356351/formatting-a-float-to-2-decimal-places/58733847#58733847 – auburg Nov 02 '22 at 09:42

1 Answers1

0

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");
DevMoth
  • 51
  • 5
  • Thanks alot, i got now! I figured out why the to string method didnt work. I just didnt define the strings under the variables part^^ – xVanDaLL Nov 02 '22 at 10:02
  • I now need the comma under each other so that they are in the same place. And the numbers which are pre the decimal point should be only 2 how do i define that – xVanDaLL Nov 03 '22 at 10:33
  • @xVanDall For that you might take a look at the `value.PadLeft()` method of strings. Something like `Console.WriteLine($"\n\tPeriodendauer in s:{s.ToString("N6").PadLeft(40, ' ')}\ts");` should do the trick. – DevMoth Nov 03 '22 at 10:51