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.

0

1 Answer 1

2
String.Format("{0:N2}", new Random().NextDouble()*10000)
Sign up to request clarification or add additional context in comments.

3 Comments

Whenever using this code please make sure to specify culture to get required result (unless you live in US).
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. " msdn.microsoft.com/en-us/library/…
Duplicate lists several variants that one can use - 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 .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.