Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert string to decimal and do computations on it

Posted on 2007-11-24
5
Medium Priority
?
1,799 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

886 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