4
Contributors
3
Replies
23
Views
1 Year
Discussion Span
Last Post by Reverend Jim
0

Using Math can help you. Also your "2.80" is wrong it will be "2.90". Try this

 Dim core As Double = 3.481
 REM: You can get or loop to your values using a loop instead of
 ' using just one example as I've done.
        Dim RoundAwayFromZero As Double = Math.Round(core, 1, MidpointRounding.AwayFromZero)
        Label1.Text = "The original value is: " & core & " The rounded value is: " & RoundAwayFromZero

I used a Label in my case.

Edited by Mr.M

1

Keep in mind that Math.Round exhibits (in my opinion) odd (no pun intended) behaviour. It behaves differently depending on whether the integer portion of a number is odd or even. For example

Math.Round(2.5) evaluates to 2.0
Math.Round(3.5) evaluates to 4.0

That's why I prefer to roll my own that always rounds x.5 up to the next integer. You can code this for any decimal place by multiplying by a factor of 10 and adding 0.5 before truncating. For example, to round 2.56 to one decimal

Multiply by 10 (to give 25.6)
Add 0.5 (to give 26.1)
Truncate (to give 26.0)
Divide by 10 (to give 2.6)

Edited by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.