Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,564 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,638 people online right now. Registration is fast and FREE... Join Now!




Convert string to decimal

 
Reply to this topicStart new topic

Convert string to decimal, Convert string to decimal, format percent and currency values and comp

TheBrain
24 Nov, 2007 - 12:22 PM
Post #1

New D.I.C Head
*

Joined: 17 Nov, 2007
Posts: 3


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Convert String To Decimal
25 Nov, 2007 - 12:57 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Use the convert method to convert string values to numerical values for calculations.

http://msdn2.microsoft.com/en-us/library/s...rt_methods.aspx

User is online!Profile CardPM
+Quote Post

TheBrain
RE: Convert String To Decimal
26 Nov, 2007 - 05:24 AM
Post #3

New D.I.C Head
*

Joined: 17 Nov, 2007
Posts: 3


My Contributions
I have the following code now: This works, but I still can't get it to format so that when I input 45 I get 45.00 I don't need the currency sign, just the 2 trailing zero's for txtretailprice.text and txtretaildisc.text and TxtDiscountprice.text


If txtRetailDisc.Text = "" Then
txtRetailDisc.Text = "0.00"
End If

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()

This post has been edited by TheBrain: 26 Nov, 2007 - 05:35 AM
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: Convert String To Decimal
26 Nov, 2007 - 06:41 AM
Post #4

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
Try this
CODE

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


The "f2" formats a floating point number with 2 decimal points.
User is offlineProfile CardPM
+Quote Post

TheBrain
RE: Convert String To Decimal
26 Nov, 2007 - 06:55 AM
Post #5

New D.I.C Head
*

Joined: 17 Nov, 2007
Posts: 3


My Contributions
Thanks, that worked for the result of discount price, but how do I format the other textboxes on validation...I tried using Parse where it says round, but get an error. I just want to be able to add those trailing zero's after the user enters a number. Should it be in Keypress or am I putting it in the wrong block of code. Besides round, what should I use?

Private Sub TxtWholesalePrice_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtWholesalePrice.Validated
Dim WholesalePrice As Decimal
TxtWholesalePrice.Text = Decimal.Round(WholesalePrice, 2).ToString("f2")

End Sub
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: Convert String To Decimal
27 Nov, 2007 - 08:23 AM
Post #6

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
I suggest using the KeyUp event. Use a Try-Catch clause with your Parse() function in the event handling code, something like
CODE

Try
   Decimal.Parse(txtRetailDisc.Text)
Catch ex As Exception
   ' display friendly error message box?
End Try

Or you can do the TryParse() function.

If you can parse it correctly, then it's a valid number. Then you need to check if it's at most two decimal places (assuming that's the case). I can only think of regular expressions. Try this
CODE

If Regex.IsMatch(txtRetailDisc.Text, "^\d+(\.\d{2})?$") Then
   ' is correct!
Else
   ' is wrong
End If

You're looking a either something like 123 or 123.4 or 123.45. The expression basically says to look for at least one digit, with optional decimal point and trailing digits. There are at most 2 trailing digits. You'd probably want to do more research on regular expression patterns...

If both tests pass, then you have a valid number that you want!
User is offlineProfile CardPM
+Quote Post

arbalu
RE: Convert String To Decimal
18 Sep, 2008 - 04:21 AM
Post #7

New D.I.C Head
*

Joined: 4 Jun, 2008
Posts: 1



Thanked: 1 times
My Contributions
You just need to put this code in TextBox1 Leave event. Try this (Original Source http://developerskb.blogspot.com/2008/09/n...-easy-with.html )

CODE

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        Dim d As Decimal = Me.TextBox1.Text
        Me.TextBox1.Text = Decimal.Round(d, 2).ToString("f2")
End Sub

wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:14PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month