本文介绍了如何使用C#舍入两个十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我将两个小数点四舍五入,

到目前为止我尝试过的地方:

Hi guys,

I was rounding up the two decimal numbers,

where i tried so far:

int amt = Convert.ToInt32(Math.Round((conc * vol) / 1000, 2));
txtamt.Text = amt.ToString();



但是,如果数字为2.5,则仅给出结果2,但
我正在寻找3

谁能帮助我.

谢谢



but, if the number is 2.5,it is giving only result as 2, but
i''m looking for 3

Can any one plzzz help me.

thanks

推荐答案

int amt = (int)Math.Round((conc * vol) / 1000, MidpointRounding.AwayFromZero);


MidpointRounding.AwayFromZero确保输入的是2.5,例如,得到3.


The MidpointRounding.AwayFromZero makes sure that you get, for example, 3 if the input is 2.5.


double amt = Convert.ToDouble(Math.Round(((conc * vol) / 1000), 2));
                decimal i = decimal.Round(Convert.ToDecimal(amt), MidpointRounding.AwayFromZero);
                txtamt.Text = i.ToString();


这篇关于如何使用C#舍入两个十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:11