CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2005
    Posts
    23

    Exclamation Splitting strings with decimal point

    Hello All,

    I have searched the previous posts but have not been able to find an answer.

    I need to be able to read an Excel string to work out what is on each side of the decimal point.

    For example in my Excel sheet C36 may contain 16.11

    I wanna be able to split the string so I have 16 in one box and 11 in the other.

    The number next time may be 6.1 so I need to end up with 6 in one box and 1 in the other.

    Thats where I have had no success with the Right(C36,2) function as in some cases I may need to use Right(C36,1) as it always varies.

    Any help would be appreciated.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Splitting strings with decimal point

    Int() wil get the whole number, and Val - Int(Val) will get the difference.
    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
    Jan 2006
    Posts
    42

    Re: Splitting strings with decimal point

    hii,
    there is one way to do it in VB which i was successful in
    First take that string into Single
    float_no = CSng(string_no)
    then take that into no into temp_integer
    intnum = (long int)float_no;
    remainder = float_no - intnum ;
    thus u have to nos Left side of decimal (whole no) is in intnum
    and right side of decimal point means the exponential is in remainder example 16.11
    intnum = 16
    remainder = .11
    u can restict the decimal points by multiplying the 11 into 10 to get 1.0 or by 100 to get 11.0
    then exponent = Int(11.0) will give u 11 -
    to convert into String just use
    Asc(16) and Asc(11) will get two strings
    if problems please tell
    yogi

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Splitting strings with decimal point

    If you don't want rounding to occur, then you have to format the output.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
     Dim num As Double, exp As Double
     num = 11.123
     MsgBox Int(num)
     exp = num - Int(num)
     MsgBox Format(exp, ".###")
    End Sub
    Gives 11, and .123
    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 2002
    Location
    Kerala
    Posts
    1,183

    Re: Splitting strings with decimal point

    Quote Originally Posted by italguy
    For example in my Excel sheet C36 may contain 16.11
    I wanna be able to split the string so I have 16 in one box and 11 in the other.
    Code:
      Dim s As String
      Dim arry() As String
      s = "16.11"
      arry = Split(s, ".", 2, vbBinaryCompare)
      MsgBox (arry(0))
      MsgBox (arry(1))
    Last edited by Sahir; February 27th, 2006 at 01:44 AM.

  6. #6
    Join Date
    Jan 2006
    Location
    Bangalore, India
    Posts
    209

    Re: Splitting strings with decimal point

    Try this,

    Code:
    num = "78:90"
    Text1.Text = Mid(num, 1, InStr(1, num, ":", vbTextCompare) - 1)
    Text2.Text = Mid(num, InStr(1, num, ":", vbTextCompare) + 1)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured