0

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
3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by Luc001
0

Hi,

You can Try something like this:

Dim result as integer
result = (textbox1 * _Units) / _priceincur
Textbox1.Text=Math.Round(result,4)

I couldn't tested.

0

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)
Else
	MessageBox.Show("Value is not decimal, please repair it.")
End If

Edited by Mitja Bonca: n/a

0

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

Math.Round(yourNumbe, numberOfDeciamls);
so:

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

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!
0

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

Hi,

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

Dim result as integer
result = (textbox1 * _Units) / _priceincur
Textbox1.Text=Math.Round(result,2)
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.