4
Contributors
3
Replies
22
Views
5 Months
Discussion Span
Last Post by Reverend Jim
Featured Replies
  • 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 … Read More

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

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.