1
Label4.Text = CDec(TextBox1.Text) * 115
Label5.Text = CInt(Label4.Text)

I want Label4 to show the exact number and Label5 to show that number without decimals and rounded up.

If I type 11,1 in the textbox, Label4 = 1276,5 and Label5 = 1276 (why rounded down?).

If I type 11,3, Label4 = 1299,5 and Label5 = 1300 (perfect).

11,5 is rounded down; 11,7 is rounded up...and on so on.

How can I make it so ,5 is always rounded up? (I am working on money-based calculations.)

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Chris
  • 13
  • 2

1 Answers1

2

If you're using VB.NET you can use Math.Round:

Label5.Text = = Math.Round(CDec(Label4.Text), MidpointRounding.AwayFromZero)

Note that MidpointRounding.AwayFromZero is necessary since the default behavior for Round is to round X.5 to the nearest even number (e.g. 1.5 rounds to 2.0, but 2.5 rounds down to 2.0).


As a side note, a minor optimization would be to store the result of the first expression in a decimal variable and reuse it rather than parsing Label4.Text again – but I didn't want that to detract from the actual answer.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
D Stanley
  • 149,601
  • 11
  • 178
  • 240