Convert string to decimal

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

Page 1 of 1

8 Replies - 173330 Views - Last Post: 03 January 2010 - 09:12 AM Rate Topic: -----

#1 TheBrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-November 07

Convert string to decimal

Post icon  Posted 24 November 2007 - 01:22 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Convert string to decimal

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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
Was This Post Helpful? 0
  • +
  • -

#3 TheBrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-November 07

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

This post has been edited by TheBrain: 26 November 2007 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#4 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Convert string to decimal

Posted 26 November 2007 - 07:41 AM

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



The "f2" formats a floating point number with 2 decimal points.
Was This Post Helpful? 0
  • +
  • -

#5 TheBrain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-November 07

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
Was This Post Helpful? 0
  • +
  • -

#6 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

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
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!
Was This Post Helpful? 0
  • +
  • -

#7 arbalu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 05-June 08

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


;)
Was This Post Helpful? 1

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2255
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Convert string to decimal

Posted 02 January 2010 - 10:38 PM

Update: From String To Decimal
Was This Post Helpful? 0
  • +
  • -

#9 dbasnett  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1