Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net ,每计算一次向下/向上舍入5次_Vb.net_Math_Decimal_Currency_Rounding - Fatal编程技术网

Vb.net ,每计算一次向下/向上舍入5次

Vb.net ,每计算一次向下/向上舍入5次,vb.net,math,decimal,currency,rounding,Vb.net,Math,Decimal,Currency,Rounding,我希望Label4显示准确的数字,Label5显示不带小数且向上舍入的数字 如果我在文本框中键入11,1,Label4=1276,5和Label5=1276(为什么向下舍入?) 如果我键入11,3,Label4=1299,5和Label5=1300(完美) 11,5向下舍入;11,7被四舍五入…以此类推 我怎样才能做到这一点,5总是四舍五入的?(我正在进行基于货币的计算。)如果您使用的是VB.NET,您可以使用Math.Round: Label4.Text = CDec(TextBox1.Tex

我希望
Label4
显示准确的数字,
Label5
显示不带小数且向上舍入的数字

如果我在文本框中键入11,1,
Label4
=1276,5和
Label5
=1276(为什么向下舍入?)

如果我键入11,3,
Label4
=1299,5和
Label5
=1300(完美)

11,5向下舍入;11,7被四舍五入…以此类推


我怎样才能做到这一点,5总是四舍五入的?(我正在进行基于货币的计算。)

如果您使用的是VB.NET,您可以使用
Math.Round

Label4.Text = CDec(TextBox1.Text) * 115
Label5.Text = CInt(Label4.Text)
请注意,
middpointrounding.AwayFromZero
是必需的,因为
Round
的默认行为是将
X.5
四舍五入到最接近的偶数(例如,1.5舍五入到2.0,但2.5舍五入到2.0)


作为旁注,一个次要的优化是将第一个表达式的结果存储在一个十进制变量中,并重用它,而不是再次解析
Label4.Text
——但我不希望这会影响实际答案

Label5.Text = = Math.Round(CDec(Label4.Text), MidpointRounding.AwayFromZero)