Solved

Convert string to decimal and do computations on it

Posted on 2007-11-24
5
1,734 Views
Last Modified: 2013-12-25
I have a vb 2005 application. Right now I am using Access as the backend database. Instead of using Currency and percentage data types in the database, I have them as text or string data types.
I am able to convert the string to decimal and format for percentage and currency so that the textboxes show the correct values like the example below:
---------------------------------------------------------------------
 Private Sub txtRetailDisc_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRetailDisc.Validated
        Dim PercentageRate As Decimal = CDec(txtRetailDisc.Text)
        txtRetailDisc.Text = FormatPercent(PercentageRate)
       End Sub
---------------------------------------------------------------------------

Here is my problem and question.
I need to take the value of txtRetailPrice and multiply it by the txtRetailDisc textbox to show the value in the txtDiscountPrice.text ( textbox)
I either can't get a result or get a mismatch when I try to do this.
Is there a way to convert a string and then do computations on it or should I change the data types in the database. I have tried that, but I cannot get Currency or percentages to format in the vb 2005 program.
I am not using any bound data controls either, this is all being done through code.
0
Comment
Question by:mindserve
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 20344429
Decimal.Parse(txtDiscountPrice.text) should convert the String to a Decimal and allow further calculations
http://msdn2.microsoft.com/en-us/library/system.decimal.parse.aspx

To round a decimal variable
Decimal.Round(VariableName, 2)
http://msdn2.microsoft.com/en-us/library/system.decimal.round.aspx

To integrate the decimal variable in a string
String.Format("The formatted decimal {0}.", VariableName)

I hope this information is helpful.

0
 
LVL 1

Author Comment

by:mindserve
ID: 20344471
Well, Microsoft has alot of info but I need an example of how to do this. Do you know of any?
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 125 total points
ID: 20344584
Sorry about that, I should have been more specific! Here is an example:
Dim RetailPrice As Decimal = Decimal.Parse(txtRetailPrice.Text)

        Dim RetailDisc As Decimal = Decimal.Parse(txtRetailDisc.Text) / 100

        Dim DiscountResult As Decimal

        Dim DiscountValue As Decimal
 

        DiscountValue = RetailPrice * RetailDisc

        DiscountResult = RetailPrice - DiscountValue

        txtDiscountPrice.Text = Decimal.Round(DiscountResult, 2).ToString()

Open in new window

0
 
LVL 1

Author Comment

by:mindserve
ID: 20345411
I keep getting errors for some reason. Says Input string is not in the correct format...
0
 
LVL 1

Author Comment

by:mindserve
ID: 20345451
Ok, I had to remove the previous formatting I had entered. Now it works.
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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

947 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

21 Experts available now in Live!

Get 1:1 Help Now