Solved

Convert string to decimal and do computations on it

Posted on 2007-11-24
5
1,746 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 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