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.
Re: Splitting strings with decimal point
Int() wil get the whole number, and Val - Int(Val) will get the difference.
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
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
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))
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)