parse string into decimal variable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: parse string into decimal variable

  1. #1
    Join Date
    Sep 2010
    Posts
    23

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: parse string into decimal variable

    That would be the INTEGER portion.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2010
    Posts
    23

    Re: parse string into decimal variable

    Quote Originally Posted by dglienna View Post
    That would be the INTEGER portion.
    Thanks for your reply, however i do not understand it.

    A code sample would help me alot.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2005
    Posts
    198

    Re: parse string into decimal variable

    Code:
    a = CDec(b)
    or
    Code:
    a = System.Convert.ToDecimal(b)
    if you don't like the VB-ish casting
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: parse string into decimal variable

    Sure about that?

    Code:
    a = CDec("Foo")
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Aug 2005
    Posts
    198

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  8. #8
    Join Date
    Jan 2011
    Posts
    4

    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 06:53 AM.

  9. #9
    Join Date
    Sep 2010
    Posts
    23

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: parse string into decimal variable

    What's 169.95 - 169?

    Isn't the Integer part 169?

    Isn't that SUBTRACTION?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Aug 2005
    Posts
    198

    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.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  12. #12
    Join Date
    Jan 2002
    Posts
    195

    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.

  13. #13
    Join Date
    Sep 2010
    Posts
    23

    Re: parse string into decimal variable

    Quote Originally Posted by David Anton View Post
    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.

  14. #14
    Join Date
    Aug 2005
    Posts
    198

    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'.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  15. #15
    Join Date
    Sep 2010
    Posts
    23

    Re: parse string into decimal variable

    Quote Originally Posted by David Anton View Post
    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 ?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center