Solved

Formatting Decimal Places in Visual Basic 2008

Posted on 2012-03-09
5
395 Views
Last Modified: 2012-06-21
I am trying to limit the decimal places in various TextBoxes to two.  "Currency" I have tried a few ways unsuccessfully.

I tried this Code:

 Private Sub BtnNet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNet.Click

        TB_NetPay.Text = Format(TB_NetPay.Text, "#0.00")
        TB_NetPay.Text = (TBGP.Text - TBT.Text - TextBox0.Text - Medicare2.Text - TextBox5.Text)


    End Sub
0
Comment
Question by:jampost
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37702546
try:
   TB_NetPay.Text = Format(TB_NetPay.Text, "c")
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37702559
Your second line overwrites whatever the first line has written. On top of that, you are relying on implicit conversion, which is almost always never a good idea. My suggestion would be to refactor your code a tad:

Private Sub BtnNet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNet.Click
    Dim decTBGP As Decimal
    Dim decTBT As Decimal
    Dim decTB0 As Decimal
    Dim decTBMedCare As Decimal
    Dim decTB5 As Decimal
    
    If Decimal.TryParse(TBGP.Text, decTBGP) AndAlso _
       Decimal.TryParse(TBT.Text, decTBT) AndAlso _
       Decimal.TryParse(TextBox0.Text, decTB0) AndAlso _
       Decimal.TryParse(Medicare2.Text, decTBMedCare) AndAlso _
       Decimal.TryParse(TextBox5.Text, decTB5) Then
       
        Dim total As Decimal = (decTBGP - decTBT - decTB0 - decTBMedCare - decTB5)
    
        TB_NetPay.Text = total.ToString("#,##0.00")    
    
End Sub

Open in new window

0
 

Author Comment

by:jampost
ID: 37702563
ged325

TB_NetPay.Text = Format(TB_NetPay.Text, "c")    had no effect, but it did not cause any errors.
0
 

Author Closing Comment

by:jampost
ID: 37702595
kaufmed:


 Thank you.  I think what you are saying about implicit conversion is something I realy need to work on. Thanks again  -  You knocked it out of the park, as usual.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37702604
NP. Glad to help  = )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change data in datatable 8 30
Events in static methods 3 50
Passing a form as a parameter to an external class 1 27
Get size of each directory on each mapped drive 5 21
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now