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.
Convert string to decimalConvert string to decimal, format percent and currency values and comp
Page 1 of 1
8 Replies - 147971 Views - Last Post: 03 January 2010 - 09:12 AM
Replies To: Convert string to decimal
#2
Re: Convert string to decimal
Posted 25 November 2007 - 01:57 PM
Use the convert method to convert string values to numerical values for calculations.
http://msdn2.microso...rt_methods.aspx
http://msdn2.microso...rt_methods.aspx
#3
Re: Convert string to decimal
Posted 26 November 2007 - 06:24 AM
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()
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 November 2007 - 06:35 AM
#4
Re: Convert string to decimal
Posted 26 November 2007 - 07:41 AM
Try this
The "f2" formats a floating point number with 2 decimal points.
txtDiscountPrice.Text = Decimal.Round(DiscountResult, 2).ToString("f2")
The "f2" formats a floating point number with 2 decimal points.
#5
Re: Convert string to decimal
Posted 26 November 2007 - 07:55 AM
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
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
#6
Re: Convert string to decimal
Posted 27 November 2007 - 09:23 AM
I suggest using the KeyUp event. Use a Try-Catch clause with your Parse() function in the event handling code, something like
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
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!
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
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!
#7
Re: Convert string to decimal
Posted 18 September 2008 - 05:21 AM
You just need to put this code in TextBox1 Leave event. Try this (Original Source http://developerskb....-easy-with.html )
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
#9
Re: Convert string to decimal
Posted 03 January 2010 - 09:12 AM
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
End Sub
Dim simVal As Decimal = 0.333D, aVal As Decimal
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'showing correct conversion checking
If Not Decimal.TryParse(TextBox1.Text, aVal) Then
MessageBox.Show("Number invalid")
Exit Sub
End If
'Doesn't format round???????
TextBox1.Text = formatTxt(simVal, 2)
simVal += 0.333D
End Sub
Private Function formatTxt(ByVal aNum As Decimal, ByVal numDec As Integer) As String
Dim fmtSTR As String = "F" & numDec.ToString
formatTxt = aNum.ToString(fmtSTR)
End Function
End Class
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|