Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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 为什么math.round不舍入到2位数?_Vb.net_Math_Rounding - Fatal编程技术网

Vb.net 为什么math.round不舍入到2位数?

Vb.net 为什么math.round不舍入到2位数?,vb.net,math,rounding,Vb.net,Math,Rounding,以下VB.Net代码未返回预期结果: Dim sum As Single = 535353 Dim vat As Single = Math.Round(sum * 0.19, 2) Dim total As Single = Math.Round(sum + vat, 2) Debug.WriteLine(String.Format("sum: {0:c2}, vat: {1:c2}, total: {2:c2}", sum,

以下VB.Net代码未返回预期结果:

        Dim sum As Single = 535353
        Dim vat As Single = Math.Round(sum * 0.19, 2)
        Dim total As Single = Math.Round(sum + vat, 2)

        Debug.WriteLine(String.Format("sum: {0:c2}, vat: {1:c2}, total: {2:c2}", sum, vat, total))

增值税正确设置为101717.07,但“总计”结果为636070.063。 为什么最后一个Math.round函数返回3位数字,结果却错误?

请使用Double试试

    Dim sum As Double = 535353
    Dim vat As Double = Math.Round(sum * 0.19, 2)
    Dim total As Double = Math.Round(sum + vat, 2)
    '           sum     535353.0    Double
    '           total   637070.07   Double
    '           vat     101717.07   Double

无法复制。我得到的
总额:535353.00美元,增值税:101717.10美元,总计:$637070.10
:请注意,
Single
只有7位精度,这就是为什么所有内容都有效地四舍五入到decimal@DStanley-从何处获得“7位精度”?使用双精度而不是单精度。@dbasnett或使用
Decimal
或缩放整数(如果使用货币或其他货币)定点值。如果使用Single,您会得到OP的结果吗?@DStanley-从Math开始没有尝试过。Round返回double。