C#__ 使用长度将字符串添加到字符串中
C#__ adding string into string using length
在我的程序中,我试图为长度超过 3 的数字添加一个逗号。例如,如果数字为 1000,它应该输出 1,000。
但是,在将逗号放入第一个数字后,我找不到如何添加剩余数字。下面的代码是我得到的:
// 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? */];
}
您可以将格式化程序传递给 ToString
方法:
decimal inputValue = 0;
if (decimal.TryParse(lbl_NumResult.Text, out inputValue))
{
string answerThousand = inputValue.ToString("N", CultureInfo.InvariantCulture);
}
在我的程序中,我试图为长度超过 3 的数字添加一个逗号。例如,如果数字为 1000,它应该输出 1,000。 但是,在将逗号放入第一个数字后,我找不到如何添加剩余数字。下面的代码是我得到的:
// 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? */];
}
您可以将格式化程序传递给 ToString
方法:
decimal inputValue = 0;
if (decimal.TryParse(lbl_NumResult.Text, out inputValue))
{
string answerThousand = inputValue.ToString("N", CultureInfo.InvariantCulture);
}