-1

In my program, I'm trying to add a comma for a number that's length is more than 3. For example if number is 1000, it should output 1,000. However I can't find out how to add on remaining numbers after I put comma into first one. The code below is what I've got:

// if the answer is more than 999
string answerThousand = Convert.ToString(lbl_NumResult.Text);

if (answerThousand.Length > 3) 
{
    lbl_NumResult.Text = answerThousand[1] + "," + answerThousand[ /* What must be here to add remaining numbers? */];
}

1 Answers1

1

You could just pass a formatter to the ToString method:

decimal inputValue = 0;
if (decimal.TryParse(lbl_NumResult.Text, out inputValue))
{
    string answerThousand = inputValue.ToString("N", CultureInfo.InvariantCulture);
}

https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#NFormatString

cf_en
  • 1,661
  • 1
  • 10
  • 18