Paul - 3 months ago 23

Vb.net Question

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.

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)

Is there a better function to do this?

Answer

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
```