VB.NET 中的整数值舍入 [英] Integer value rounding in VB.NET

查看:54
本文介绍了VB.NET 中的整数值舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码:

Dim num1 As Integer
Dim num2 As Integer

num1 = 12.5
num2 = 17.5

当我输出值时,我得到:

When I output the values, I get this:

num1: 12
num2: 18

如果12.5变成1217.5应该是17吗?

或者如果17.5变成1812.5应该是13吗?

Or if 17.5 become 18, is 12.5 supposed to be 13?

我是 Visual Basic 的新手,很难找到这方面的任何参考资料.

I'm pretty new in Visual Basic, and it's hard to find any reference on this.

推荐答案

这是因为 VB 使用 银行家的舍入(取整规则),如整数文档

在这种方法中,当四舍五入位置的余数为.5时,当它前面的数字为奇数时,该数字向上取整>,并且当它前面的数字为偶数向下取整.

In this method, when the remainder at the rounding position is .5, that number is rounded up when the number before it is odd, and rounded down when the number before it is even.

例如,使用舍入到偶数规则

For example, using round-to-even rule

2.5 round down to the even number 2.0, 
3.5 would round up to the even number 4.0

这篇关于VB.NET 中的整数值舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆