943,527 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 13433
  • VB.NET RSS
Aug 12th, 2009
-1

Format to 2 decimal places

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smileyc is offline Offline
7 posts
since May 2009
Aug 12th, 2009
1

Re: Format to 2 decimal places

vb.net Syntax (Toggle Plain Text)
  1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  2. Dim bigNumber As Decimal = 1234567.123456
  3. Console.WriteLine("F2: " & bigNumber.ToString("F2"))
  4. Console.WriteLine("N2: " & bigNumber.ToString("N2"))
  5. 'Likewise:
  6. TextBox1.Text = bigNumber.ToString("F2")
  7. End Sub

Results in:
VB.NET Syntax (Toggle Plain Text)
  1. F2: 1234567.12
  2. N2: 1,234,567.12

So use F2 to not include a thousands separator, N2 will include it
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 14th, 2009
0

Re: Format to 2 decimal places

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smileyc is offline Offline
7 posts
since May 2009
Aug 14th, 2009
0

Re: Format to 2 decimal places

This should work:

VB.NET Syntax (Toggle Plain Text)
  1. label24.Text = String.Format("{0:F2}", a/b)
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008
Aug 16th, 2009
0

Re: Format to 2 decimal places

you can try this hope it will work for you.

VB.NET Syntax (Toggle Plain Text)
  1. label24.text =Math.Round(a/b, 2)


or try this

VB.NET Syntax (Toggle Plain Text)
  1. label24.text = Format(a/b, "0.00")

try both hope it works with you
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
emint is offline Offline
60 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Help about my statement
Next Thread in VB.NET Forum Timeline: Function placement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC