Solved

Convert string to decimal and do computations on it

Posted on 2007-11-24
5
1,717 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I keep getting errors for some reason. Says Input string is not in the correct format...
0
 
LVL 1

Author Comment

by:mindserve
Comment Utility
Ok, I had to remove the previous formatting I had entered. Now it works.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

763 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

8 Experts available now in Live!

Get 1:1 Help Now