-2
        Console.Write("Hoeveel worpen wil je simuleren: ");
        int worpen = int.Parse(Console.ReadLine());



        Random r = new Random(worpen);
        int willekeur = r.Next(1, worpen);
        double willekeur1 = willekeur;
        Math.Round(willekeur1);


        for (int i = 1; i <= 12; i++)
        {
            Console.WriteLine("ik gooide "+willekeur+" ("+Math.Round(willekeur1,2,)+")"); 
            willekeur = r.Next(1, worpen);

        }
        Console.ReadLine();

I want that ' willekeur1 ' a number which contains a decimal comma is. so example: 12456--> 12,456

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
akhandafm
  • 143
  • 1
  • 2
  • 9
  • 2
    Possible duplicate of [.NET String.Format() to add commas in thousands place for a number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – 001 Nov 02 '16 at 19:58
  • 1
    `Int`s don't have decimals or commas - when you convert to a string, the current culture (or a culture you specify) determines the format. Use [`string.Format(number, format)`](https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx) or [`double.ToString(format)`](https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx) to specify the format you want the result in. – D Stanley Nov 02 '16 at 19:58
  • Very unclear what you are asking (especially with something completely unrelated accepted as answer). Maybe you just looking for `willekeur1/1000.0`? – Alexei Levenkov Nov 02 '16 at 21:24

2 Answers2

0

You can do: (you need latest c# to use string interpolation)

 $"{12456:n0}"; // 12,456
 $"{12456:n2}"; // 12,456.00

In your case

Console.WriteLine($"ik gooide {willekeur} ({Math.Round(willekeur1,2,)})"); 

or

 $"{Math.Round(willekeur1,2):n0}"; 
 $"{Math.Round(willekeur1,2):n2}"; 
brakeroo
  • 1,407
  • 13
  • 24
-1

this might be useful for you:

public float ReadFloat()
        {
            float ReadValue = 0;
            string KeySequence = "";
            string TempKey = "";
            bool CommaUsed = false;
            ConsoleKeyInfo key;
            do
            {
                key = Console.ReadKey(true);
                if ((key.Key >= ConsoleKey.D0 && key.Key <= ConsoleKey.D9) || (key.Key >= ConsoleKey.NumPad0 && key.Key <= ConsoleKey.NumPad9))
                {
                    TempKey = Convert.ToString(key.Key);
                    TempKey = TempKey.Remove(0, 1);
                    KeySequence += TempKey;
                    Console.Write(TempKey);
                };

                if (key.Key == ConsoleKey.OemComma || key.Key == ConsoleKey.Decimal)
                {
                    if (!CommaUsed)
                    {
                        KeySequence += ".";
                        Console.Write(".");
                        CommaUsed = true;

                    };
                };

                if ((key.Key == ConsoleKey.Backspace) && KeySequence != "")
                {
                    string LastChar = KeySequence.Substring(KeySequence.Length - 1);
                    //MessageBox.Show("Last char: "+LastChar);
                    //Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)
                    char SepDeci = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
                    if (Convert.ToChar(LastChar) == SepDeci)
                    {
                        CommaUsed = false;
                    };
                    KeySequence = KeySequence.Remove(KeySequence.Length - 1);
                    Console.Write("\b \b");
                };
            }
            while (key.Key != ConsoleKey.Enter);
            if (KeySequence == "")
            {
                return 0;
            };
            ReadValue = Convert.ToSingle(KeySequence);
            return ReadValue;
        }

this method reads value from console but allows only numbers and one decimal separator (comma or dot, depending on your culture settings). to use is to read value to a variable:

willekeur1 = ReadFloat();
  • This post does not look like it provides an answer to question asked. Also it is very strange to propose manual code to parse numbers when built in functions exist for parsing and formatting... – Alexei Levenkov Nov 02 '16 at 21:22