That's the kind of question that you can search the web for.
vb.net string to integer site:microsoft.com
Code:
Module StringParsing
Public Sub Main()
TryToParse(Nothing)
TryToParse("160519")
TryToParse("9432.0")
TryToParse("16,667")
TryToParse(" -322 ")
TryToParse("+4302")
TryToParse("(100)")
TryToParse("01FA")
End Sub
Private Sub TryToParse(value As String)
Dim number As Integer
Dim result As Boolean = Int32.TryParse(value, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
If value Is Nothing Then value = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", value)
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '' failed.
' Converted '160519' to 160519.
' Attempted conversion of '9432.0' failed.
' Attempted conversion of '16,667' failed.
' Converted ' -322 ' to -322.
' Converted '+4302' to 4302.
' Attempted conversion of '(100)' failed.
' Attempted conversion of '01FA' failed.
You're right - it's not idiot-proof.
It will only work when you can reasonably assume that the argument is the string version of a decimal number.
If the argument is likely to be any junk then you'll have to use your approach or Decimal.TryParse.
However, in that case I think the flaw is how you allowed "Foo" as an input in the first place - if you're reading the input from a textbox, for instance, you would never allow "Foo" as an input - you would filter the input to prevent "Foo" or use a masked textbox.
David Anton
Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
You can...
Dim firststr As String = "hello"
Dim firstdec As Decimal = 1.5
Dim result As String = Decimal.Parse(firstdec)
Dim Themain As New String(result + firststr)
Console.WriteLine(Themain)
Console.ReadLine()
Converting dec to string example
If joining the two lines.,... im new so I may be wrong
Last edited by rsutton84; February 8th, 2011 at 06:53 AM.
I thank all of you for your responses, however all solutions mentioned, are incorrect.
dim a as string = "169.65"
dim b as decimal = System.Convert.ToDecimal(a)
' Now b = 16965
dim a as string = "169.65"
dim b as decimal = CDec(a)
' Now b = 16965 (no dot)
dim a as string = "169.65"
dim b as decimal = Decimal.Parse(a)
' Now b = 16965 (no dot)
dim a as decimal = 169.65
' Now a = 169.65 (there is a dot) This is what i want.
I am parsing a TEXT file, into decimal variables.
There is no way you can distinguish between 5.85 and 585, if that dot gets removed every
time. There has got to be a simple way of getting a decimal value parsed into a decimal
variable without it removing the dot, especially when "dim a as decimal = 169.65 " leaves the
dot in place.
The dot is being remove some other way in your code.
CDec and System.Convert.ToDecimal certainly do not remove the dot.
If the string contains a valid decimal, then that decimal value is extracted via CDec or System.Convert.ToDecimal.
David Anton
Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
The dot is being remove some other way in your code.
CDec and System.Convert.ToDecimal certainly do not remove the dot.
If the string contains a valid decimal, then that decimal value is extracted via CDec or System.Convert.ToDecimal.
Bookmarks