dcsimg
Results 1 to 3 of 3

Thread: Integer with no decimal places

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    2

    Integer with no decimal places

    Hi Guys
    My problem can be seen from code below:
    Dim x as string ="" ( string gets populated by regex and there is a decimal place causing failure if declared as integer
    Dim y as integer = 17.35
    x= cint(x) + y

    Tried "
    x=Math.Round(Convert.ToDecimal(x), 2)
    If i declare y as string output = x & y (concatenate)

    output is always 17
    I need it to be 17.35
    Any idees
    Regards
    Cobus

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Integer with no decimal places

    Welcome to VBForums

    Use appropriate data types for what you want to do at each stage. If you want a number with decimal places then it needs to be Single/Double (fast but not totally accurate) or Decimal (slower, but always accurate). An Integer can only contain whole numbers (so not 17.35).

    You should also use more variables if it helps, such as creating a "result" variable, eg:
    Code:
    Dim x as string ="" ( string gets populated by regex and there is a decimal place causing failure if declared as integer
    Dim y as Decimal = 17.35
    Dim result as Decimal
      result = y + Math.Round(Convert.ToDecimal(x), 2)
    If you want to display the result somewhere text based (such as in a Label or Textbox), then convert it ToString with a format if you care about it at all (because otherwise numbers might sometimes display differently), eg:
    Code:
     resultTextBox.Text = "The value is: " & result.ToString("N2")

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    2

    Re: Integer with no decimal places

    Quote Originally Posted by si_the_geek View Post
    Welcome to VBForums

    Use appropriate data types for what you want to do at each stage. If you want a number with decimal places then it needs to be Single/Double (fast but not totally accurate) or Decimal (slower, but always accurate). An Integer can only contain whole numbers (so not 17.35).

    You should also use more variables if it helps, such as creating a "result" variable, eg:
    Code:
    Dim x as string ="" ( string gets populated by regex and there is a decimal place causing failure if declared as integer
    Dim y as Decimal = 17.35
    Dim result as Decimal
      result = y + Math.Round(Convert.ToDecimal(x), 2)
    If you want to display the result somewhere text based (such as in a Label or Textbox), then convert it ToString with a format if you care about it at all (because otherwise numbers might sometimes display differently), eg:
    Code:
     resultTextBox.Text = "The value is: " & result.ToString("N2")
    Working
    Thanks and great answer
    Regards
    Cobus

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width