I am doing some calculations and the outcome of those sums are like 0.743449889958

the sum is calulated like so

textbox.Text = (textbox1 * _Units) / _priceincur

how would i format the string so that it only shows 0.7434 (rounded)

Attachments Picture1.jpg 18.32 KB


You can Try something like this:

Dim result as integer
result = (textbox1 * _Units) / _priceincur

I couldn't tested.

As said, use Math.Round method. 1st parameter in this method in the actual value you want to round, while 2nd parameter is the number of decimals.
If you will do as Luc001 showed, will do just fine or:

Dim dValue As Decimal = 0D
If Decimal.TryParse(textBox1.Text.Trim(), dValue) Then
	textBox2.Text = Math.Round((dValue * _Units / _priceincur), 4)
	MessageBox.Show("Value is not decimal, please repair it.")
End If

Edited 5 Years Ago by Mitja Bonca: n/a

I have explained how to set number of decimals. Have you read my post for above?

Math.Round(yourNumbe, numberOfDeciamls);

textBox2.Text = Math.Round((dValue * _Units / _priceincur), 2)

and btw, about math priorities, and using brackets:
multiplying has an advantage over dividing, so you do not need any bracket when you do:

A*B\C => this means A will be multiplied with B in the 1st place, then will be divided by C!

my fault, i only want this to two decimal places. IE 0.74


Just to show you how you can change my snippet into a 2 decimal output:

Dim result as integer
result = (textbox1 * _Units) / _priceincur
This article has been dead for over six months. Start a new discussion instead.