1.11M Members

Format to 2 decimal places

 
-1
 

Hi, sorry to ask this, I know it has been asked before but despite reading the answers I still dont get it. I am using visual studio 2008, vb.net.
I have a lable, label1. I want it to display the results of a calculation, a/b. The answer is a number typically 0.xxxxxxxxxx . I want the answer displayed to two decimal places in lable1. How do I achieve that please?

Thanks for all and any help.

 
1
 
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim bigNumber As Decimal = 1234567.123456
    Console.WriteLine("F2: " & bigNumber.ToString("F2"))
    Console.WriteLine("N2: " & bigNumber.ToString("N2"))
    'Likewise:
    TextBox1.Text = bigNumber.ToString("F2")
End Sub

Results in:

F2: 1234567.12
N2: 1,234,567.12

So use F2 to not include a thousands separator, N2 will include it

 
0
 

Thank you for your reply, but the answer does not seem to be working for me.

In my code I have

dim a as double
dim b as double
'
'some calculation here wich typically results in 
'a/b being 0.xxxxxxx
'

label24.text =a/b

And I cannot get the text in label24 to format to 2 decimal places.
What am I doing wrong or have not grasped please. THank you again

 
0
 

This should work:

label24.Text = String.Format("{0:F2}", a/b)
 
0
 

you can try this hope it will work for you.

label24.text =Math.Round(a/b, 2)

or try this

label24.text = Format(a/b, "0.00")

try both hope it works with you

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article