Format question

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
K, I need a number formated so it has no decimal places, and that it rounds down. how do i do that?
 
Give this a shot:

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myDec As Decimal = 9.75453
        Dim myInt As Integer = 0

        myInt = CInt(Decimal.Floor(myDec))
End Sub
 
Math.Round(myDec, 2) would work as well


Sounds like Jugger found something else, though I'm not quite sure what is meant in this reference to "Integer Division" but...either way, in this case I dont think Math.Round(myDec,2) would work would it?

Aside from the replacing the 2 with a 0 to achive the no decimal places he mentioned, is there a way to have Math.Round always round down? Seems like AwayFromZero and NearestEven are the only options.
 
Math.Floor (might be Math.ToFloor) rounds down to the nearest whole number (droppes the decimal altogether) just like Math.Ceiling rounds up to the next whole number [which actually it does this: Math.Round(YourNumber + 0.499999, 0) to get the next whole number]

one thing to remember is that Math.Round uses true rounding (statistical rounding) which means if it hits a 5 it'll round to the next even number, IE:
.5 rounds to 0
1.5 rounds to 2
2.5 rounds to 2
3.5 rounds to 4
etc....
 
If you use \ instead of /, it will round your answer down, without decimal places. My teacher told me that its called integer division.
 
Ah, gotcha. I think I recall something about that now, I was picturing something -completly- different =) Sorry for the confusion.
 
Conversion to an Integer (floating point truncation) would be what programmers call it..

Dim i as integer = 5 \ 2

or

Dim i as Integer = Convert.ToInt32(5 / 2)
 
Back
Top