Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert string to decimal and do computations on it

Posted on 2007-11-24
5
Medium Priority
?
1,790 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Suggested Courses

670 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