如何使用特定参数对零进行计数

本文关键字:何使用 参数 | 更新日期: 2023-09-27 18:00:51

我需要计算一些零。到目前为止,这个代码是有效的:

 private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (uFCheckBox.Checked == true)
            {
                nFCheckBox.Checked = false;
                pFCheckBox.Checked = false;
                decimal x = 0;
                if (Decimal.TryParse(textBox1.Text, out x))
                {
                    var y = 1000000;
                    var answer = x * y;
                    displayLabel2.Text = (x.ToString().Replace(".", "").TrimStart(new Char[] { '0' }) + "00").Substring(0, 2);
                    string myString = answer.ToString();
                     // displayLabel5.Text = myString.Split('.')[0].Where(d => d == '0').Count().ToString();
                    displayLabel5.Text = myString.Split('.')[0].Where(d => d == '0').Count().ToString();
                }

当我输入72、47、83等数字时,它会完美地计算零。但一旦我输入以零结尾的数字,它就会计算零。我需要一个在前两位数字后全部为零的东西。所以50 x 1000000等于50000000。但我不需要计算前两位数字,所以在这种情况下我需要它输出6。更多示例:

1.0 x 1,000,000 = 1,000,000 - I only need to output 5 zeroes
0.10 x 1,000,000 = 100,000 - I only need to output 4 zeroes.

但我也需要保持,如果我输入其他不以零结尾的数字,它仍然可以正常计数。示例:

72 x 1,000,000 = 72,000,000 - Needs to output 6
7.2 x 1,000,000 = 7,200,000 - Needs to output 5
.72 x 1,000,000 = 720,000 - Needs to output 4

更新:当我使用时,我现在得到了正确的输出

decimal n = str.Split('.')[0].Substring(2, str.Length - 2).Count( s => s == '0');

但现在我得到一个错误:"索引和长度必须引用字符串中的一个位置。参数名称:length">

如何使用特定参数对零进行计数

如果我理解正确,你只希望它输出零的数量。要做到这一点,您需要执行以下操作:

var y = 1000000;
var answer = x * y;
string numString = answer.ToString();
char[] charArray = numString.ToCharArray();
int count = 0;
for(int i = 2; i < charArray.Length; i++)
{
     if(charArray[i] == '0')
     {
          count++;
     }
}
string output = count.ToString();

使用此选项,输出将是前两位数字后的0的字符串计数。

var y = 1000000;
var answer = x * y;
var str= answer.ToString();
var n = str.Substring(2, str.Length - 2).Count(s => s == '0');