marwan akhandaf - 1 year ago 123
C# Question

# How to convert int to a decimal with comma?

``````        Console.Write("Hoeveel worpen wil je simuleren: ");

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);

}
``````

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

this might be useful for you:

``````public float ReadFloat()
{
string KeySequence = "";
string TempKey = "";
bool CommaUsed = false;
ConsoleKeyInfo key;
do
{
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;
};
``````willekeur1 = ReadFloat();