0

:)

What should I write to make the answer be shown as 2 or 3 decimals at the end? Thinking it's something with Math.Round but don't know what..

namespace Uppgift_1___Avrundaren
{
    class Program
    {
        static void Main(string[] args)
        {
            string tal1;
            double tal2;
            int decimaler2;
            string decimaler1;
            Console.WriteLine("Avrundaren");
            Console.WriteLine();
            Console.WriteLine("Hej! Skriv in ett valfritt tal i decimalform:");
            tal1 = Console.ReadLine();
            tal2 = double.Parse(tal1);
            Console.WriteLine("Skriv in hur många decimaler som önskas:");
            decimaler1 = Console.ReadLine();
            decimaler2 = int.Parse(decimaler1);
            Console.Write("Dags att avrunda! Svaret blir: ");
            Console.WriteLine(Math.Round(tal2, decimaler2));
            Console.ReadKey();

        }
    }
}
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
Chrilla
  • 21
  • 1
  • 6
  • you mean this part `Console.WriteLine(Math.Round(tal2, decimaler2));`..? i think you can do something like `Console.WriteLine(Math.Round(tal2, decimaler2).ToString("#.###"));`. cmiiw. – Bagus Tesa Mar 06 '19 at 19:04
  • 3
    Possible duplicate of [How do I round a decimal value to 2 decimal places (for output on a page)](https://stackoverflow.com/questions/164926/how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-page) – Bagus Tesa Mar 06 '19 at 19:05
  • What is wrong with your code? –  Mar 06 '19 at 19:06
  • "_What should I write to make the answer be shown as 2 or 3 decimals at the end?_" You should then input 2 or 3 when the program asks "_Skriv in hur många decimaler som önskas:_".... –  Mar 06 '19 at 19:08
  • Use this to print out your output `Console.WriteLine(Math.Round(tal2, decimaler2).ToString("F"));` – Rahul Sharma Mar 06 '19 at 19:08
  • Thanks alot guys! Worked great :) Totally new to all this and still trying to learn! – Chrilla Mar 06 '19 at 19:40

0 Answers0