Solved

Formatting Decimal Places in Visual Basic 2008

Posted on 2012-03-09
5
399 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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