在Visual Basic中将数字四舍五入 [英] Rounding a number down in Visual Basic

查看:102
本文介绍了在Visual Basic中将数字四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Visual Basic应用程序,需要将数字四舍五入,例如2.556将变为2.55,而不是2.26.

I have a Visual Basic application that needs to round a number down, for example, 2.556 would become 2.55 and not 2.26.

我可以使用以下功能来执行此操作:使用以下功能从小数点开始去除大于2的字符:

I can do this using a function to strip off the characters more than 2 right from the decimal point using this:

Dim TheString As String
TheString = 2.556
Dim thelength = Len(TheString)
Dim thedecimal = InStr(TheString, ".", CompareMethod.Text)
Dim Characters = thelength - (thelength - thedecimal - 2)
_2DPRoundedDown = Left(TheString, Characters)

有更好的功能吗?

推荐答案

您可以使用

You can do this with Math.Floor. However, you'll need to multiply * 100 and divide, since you can't supply a number of digits

Dim theNumber as Double
theNumber = 2.556
Dim theRounded = Math.Sign(theNumber) * Math.Floor(Math.Abs(theNumber) * 100) / 100.0

这篇关于在Visual Basic中将数字四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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