表格内,当前行库存,想在小数点后面只保留两位小数;

请教一下,我做表格的结存数量,公式是:上一行的结余+当前行的入库-当前行的出库= 当前行的库存;
并且设置的当前行库存,只保留两位小数,但在实际计算中,还有些数据小数点后的位数很多,请问是什么原因呢,以下是代码和截图,请给指导一下;

img

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {                    

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (e.RowIndex < 1)
                {
                   MessageBox.Show("请从第二行录入数据");

                   return;
                }
                
                string PreMaterial = dataGridView1.Rows[e.RowIndex - 1].Cells[2].Value.ToString();

                string CurMaterial = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

                if (PreMaterial != CurMaterial )
                {
                    return;
                }

                else
                {                

                    if (e.RowIndex > 0) // 不是第一行
                    {
                        int rowIndex = e.RowIndex;

                        float 上一行结存数量 = Convert.ToSingle(dataGridView1.Rows[rowIndex - 1].Cells[6].Value);

                        float 当前行入库数量, 当前行出库数量;

                        if (dataGridView1.Rows[rowIndex].Cells[4].Value == DBNull.Value)
                        {
                            当前行入库数量 = 0;
                        }

                        else
                        {
                            当前行入库数量 = Convert.ToSingle(dataGridView1.Rows[rowIndex].Cells[4].Value);
                        }

                        if (dataGridView1.Rows[rowIndex].Cells[5].Value == DBNull.Value)
                        {
                            当前行出库数量 = 0;
                        }

                        else
                        {
                            当前行出库数量 = Convert.ToSingle(dataGridView1.Rows[rowIndex].Cells[5].Value);
                        }

                        float 当前行结存数量 = 上一行结存数量 + 当前行入库数量 - 当前行出库数量;

                        当前行结存数量 = (float)Math.Round(当前行结存数量, 2);

                        dataGridView1.Rows[rowIndex].Cells[6].Value = 当前行结存数量.ToString("0.00");
                    }                   
                }
            }                             
        }

我将数据表里面,对应的字段的类型改为decimal,就可以了;

【以下回答由 GPT 生成】

问题的原因是在计算结果时,使用了float数据类型,并且使用了Math.Round方法来保留两位小数。然而,float数据类型本身精度有限,并且在进行计算时可能会出现精度丢失的情况。

为了解决这个问题,我们可以使用decimal数据类型来进行计算,并且使用ToString方法来格式化保留两位小数。

下面是修改后的代码:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{                    
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (e.RowIndex < 1)
        {
           MessageBox.Show("请从第二行录入数据");
           return;
        }

        string PreMaterial = dataGridView1.Rows[e.RowIndex - 1].Cells[2].Value.ToString();
        string CurMaterial = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

        if (PreMaterial != CurMaterial )
        {
            return;
        }

        else
        {                

            if (e.RowIndex > 0) // 不是第一行
            {
                int rowIndex = e.RowIndex;

                decimal 上一行结存数量 = Convert.ToDecimal(dataGridView1.Rows[rowIndex - 1].Cells[6].Value);
                decimal 当前行入库数量, 当前行出库数量;

                if (dataGridView1.Rows[rowIndex].Cells[4].Value == DBNull.Value)
                {
                    当前行入库数量 = 0;
                }
                else
                {
                    当前行入库数量 = Convert.ToDecimal(dataGridView1.Rows[rowIndex].Cells[4].Value);
                }

                if (dataGridView1.Rows[rowIndex].Cells[5].Value == DBNull.Value)
                {
                    当前行出库数量 = 0;
                }
                else
                {
                    当前行出库数量 = Convert.ToDecimal(dataGridView1.Rows[rowIndex].Cells[5].Value);
                }

                // 计算当前行的库存
                decimal 当前行库存 = 上一行结存数量 + 当前行入库数量 - 当前行出库数量;

                // 保留两位小数
                string 当前行库存保留两位小数 = 当前行库存.ToString("0.00");

                // 设置当前行的库存值
                dataGridView1.Rows[rowIndex].Cells[6].Value = 当前行库存保留两位小数;
            }
        }
    }
}

调整后的代码中,我使用了decimal数据类型来定义上一行结存数量、当前行入库数量和当前行出库数量,并且使用ToString方法来将计算结果保留两位小数。然后将结果设置给当前行的库存单元格。

请尝试使用以上修改后的代码来解决你的问题。如果还有其他问题,请随时提问。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^