Format to 2 decimal places

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 7
Reputation: smileyc is an unknown quantity at this point 
Solved Threads: 0
smileyc smileyc is offline Offline
Newbie Poster

Format to 2 decimal places

 
0
  #1
Aug 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,437
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 624
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Format to 2 decimal places

 
0
  #2
Aug 12th, 2009
  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:
  1. F2: 1234567.12
  2. N2: 1,234,567.12

So use F2 to not include a thousands separator, N2 will include it
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: smileyc is an unknown quantity at this point 
Solved Threads: 0
smileyc smileyc is offline Offline
Newbie Poster

Re: Format to 2 decimal places

 
0
  #3
Aug 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

Re: Format to 2 decimal places

 
0
  #4
Aug 14th, 2009
This should work:

  1. label24.Text = String.Format("{0:F2}", a/b)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 59
Reputation: emint is an unknown quantity at this point 
Solved Threads: 4
emint emint is offline Offline
Junior Poster in Training

Re: Format to 2 decimal places

 
0
  #5
Aug 16th, 2009
you can try this hope it will work for you.

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


or try this

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

try both hope it works with you
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 2223 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC