Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Formatting Decimal Places in Visual Basic 2008

Posted on 2012-03-09
5
Medium Priority
?
403 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 41

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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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