Howdy,
dim a as decimal
dim b as string = "169.65"
How do i get b into a, without removing the dot ?
Thanks.
Printable View
Howdy,
dim a as decimal
dim b as string = "169.65"
How do i get b into a, without removing the dot ?
Thanks.
That would be the INTEGER portion.
That's the kind of question that you can search the web for.
Quote:
vb.net string to integer site:microsoft.com
http://msdn.microsoft.com/en-us/library/f02979c7.aspxCode: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.
orCode:a = CDec(b)
if you don't like the VB-ish castingCode:a = System.Convert.ToDecimal(b)
Sure about that?
Code:a = CDec("Foo")
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.
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
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.
Every method, removes the dot.
What's 169.95 - 169?
Isn't the Integer part 169?
Isn't that SUBTRACTION?
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.
dim a as Single
dim b as string = "169.65"
Convert.ToSingle(b)
The single point data type and conversion will maintian your decimal place. At least it does on my computer.
what other code? Its only 4 lines:
Quote:
Dim a As Single
Dim b As String = "169.65"
a = Convert.ToSingle(b)
Button1.Text = a
http://img135.imageshack.us/img135/7727/naamloosuu.png
Uploaded with ImageShack.us
Same with every other method mentioned, The dot gets removed or at least moved to some other spot, with every other method mentioned.
You've got something weird happening on your system or some bad locale setting.
When I try the same code I get 169.65 assigned to 'a'.