Solved

Formatting Decimal Places in Visual Basic 2008

Posted on 2012-03-09
5
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

690 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