1

I am trying to reduce numbers

Dim x As String = 7 + 5 + 5 / 3
TextBox1.Text = x

the result will be 1.66666666666667.
I want to reduce it to exactly 1.6.

I tried this:

Dim x As String = 5
TextBox1.Text = String.Format("{0:N}", x / 3)

that reduces it to 1.667.

But I want to reduce it to only one number after point, like 1.6

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Ashraf Eldawody
  • 138
  • 3
  • 11

2 Answers2

3

You can use Math.Truncate:

Dim d As Decimal = 5 / 3
TextBox1.Text = (Math.Truncate(d * 10) / 10).ToString

Also, consider turning option strict on because you shouldn't be feeding equations into text properties like that...and you may want to clarify that first equation in your question because it doesn't evaluate to 1.66666666666667 anyway

soohoonigan
  • 2,342
  • 2
  • 10
  • 18
0

You can also use Math.Round

TextBox1.Text =  Math.Round(7 + 5 + 5 / 3, 2) 

Where 2 is the number of decimals that you want to show.

Juanche
  • 104
  • 2
  • 8