-
February 7th, 2011, 04:59 AM
#1
parse string into decimal variable
Howdy,
dim a as decimal
dim b as string = "169.65"
How do i get b into a, without removing the dot ?
Thanks.
-
February 7th, 2011, 12:15 PM
#2
Re: parse string into decimal variable
That would be the INTEGER portion.
-
February 7th, 2011, 12:41 PM
#3
Re: parse string into decimal variable
Originally Posted by dglienna
That would be the INTEGER portion.
Thanks for your reply, however i do not understand it.
A code sample would help me alot.
-
February 7th, 2011, 12:59 PM
#4
Re: parse string into decimal variable
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.
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
-
February 7th, 2011, 05:58 PM
#5
Re: parse string into decimal variable
or
Code:
a = System.Convert.ToDecimal(b)
if you don't like the VB-ish casting
-
February 7th, 2011, 10:42 PM
#6
Re: parse string into decimal variable
-
February 7th, 2011, 11:26 PM
#7
Re: parse string into decimal variable
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.
-
February 8th, 2011, 07:49 AM
#8
Re: parse string into decimal variable
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 07:53 AM.
-
February 8th, 2011, 03:54 PM
#9
Re: parse string into decimal variable
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.
-
February 8th, 2011, 04:13 PM
#10
Re: parse string into decimal variable
What's 169.95 - 169?
Isn't the Integer part 169?
Isn't that SUBTRACTION?
-
February 8th, 2011, 04:14 PM
#11
Re: parse string into decimal variable
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.
-
February 8th, 2011, 04:40 PM
#12
Re: parse string into decimal variable
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.
-
February 9th, 2011, 01:52 AM
#13
Re: parse string into decimal variable
Originally Posted by David Anton
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.
what other code? Its only 4 lines:
Dim a As Single
Dim b As String = "169.65"
a = Convert.ToSingle(b)
Button1.Text = a
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.
-
February 9th, 2011, 05:04 PM
#14
Re: parse string into decimal variable
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'.
-
February 10th, 2011, 03:02 PM
#15
Re: parse string into decimal variable
Originally Posted by David Anton
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'.
Just my luck.
This is a default visual basic installation. Any tips on how to fix this ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|