如何解决我正试图最好地计算小计的增值税和小计
我正试图最好地计算小计,增值税百分比和小计,但我不知道我在这里错过了什么。我所知道的数量100和增值税15%的正确计算是:小计= 86.96,增值税= 13.04,总计= 100(含增值税)。 但是我的代码给了我另一个结果。那么正确的代码是什么,我错过了什么?
private void dataGridView1_CellEndEdit(object sender,DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) *
Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
}
//datagridview column total in label
decimal sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sum += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
}
lblSum.Text = sum.ToString();
//vat
decimal vat = (sum * 15 / 100);
lblVat.Text = vat.ToString();
//subtotal
decimal subtotal = (sum - vat);
lblSubtotal.Text = subtotal.ToString();
解决方法
如果您尝试从包含价格(包括增值税)的总和中获得增值税间隔,则应更改此行
decimal vat = (sum * 15 / 100);
到
decimal vatpct = 15m;
decimal vat = Math.Round(sum - (sum / (1 + ( vatpct / 100))),2);
或简单地解释
decimal vatpct = 1 + (100 / 15m);
decimal vat = sum - Math.Round((sum / vatpct),2);
当然,请记住从某些配置中加载该15常量,因为该值更改过频繁。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。