0

Currently I have this code:

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = (new Random().NextDouble() * (new Random()).Next(10000)).ToString("0.00");
    //if (this.label1.Text.Length >= 7)
    //{
    //    this.label1.Text = string.Format("{0:0,0}", label1.Text);
    //}
}

which generated random number from 1-10000. I want to insert comma like this: 9,677.89.

NASSER
  • 5,900
  • 7
  • 38
  • 57
ajbee
  • 3,511
  • 3
  • 29
  • 56

1 Answers1

2
String.Format("{0:N2}", new Random().NextDouble()*10000)
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Whenever using this code please make sure to specify culture to get required result (unless you live in US). – Alexei Levenkov Sep 03 '15 at 04:33
  • 1
    Is the multiplication with larger integer and later division by double necessary? NextDouble already return double and "If one of the operands in a mathematical operation is a numeric type other than a Double, it is converted to a Double before performing the operation. The result of the operation is also a Double value. " https://msdn.microsoft.com/en-us/library/system.double%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – Martheen Sep 03 '15 at 04:42
  • Duplicate lists several variants that one can use - http://stackoverflow.com/a/15668208/477420 if you need the same separators all the time - use invariant, if need particular one - specify explicitly, or use current as your code shows . – Alexei Levenkov Sep 03 '15 at 06:20