437,594 Members | 2,059 Online Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

# How to get Decimal Points in Numeric Value

 100+ P: 115 Hi everyone! I am entering only numbers with decimal points/values in a TEXTBOX on the VB form. I need to get only the decimal values. How can I? Please help me as I tried in this way (for 2 Decimal places) Expand|Select|Wrap|Line Numbers  VB6         Dim x As Double         Dim dc As Integer, dcStr As String           x = Val(Text1.Text)         dc = InStr(x, ".")         If dc > 0 Then             dcStr = Left(Mid(Text1.Text, dc + 1) & "00", 2)         End If   ThnQ all ! Nov 22 '07 #1
6 Replies

 100+ P: 115 Hi everyone! I am entering only numbers with decimal points/values in a TEXTBOX on the VB form. I need to get only the decimal values. How can I? Please help me as I tried in this way (for 2 Decimal places) : : ThnQ all ! Also can I restrict to enter only 2 decimal places? I tried this code in TEXT_CHANGE() Expand|Select|Wrap|Line Numbers         Dim x As Double         Dim dc As Integer, dcStr As String           x = Val(Text1.Text)         dc = InStr(x, ".")         If dc > 0 Then             dcStr = Left(Mid(Text1.Text, dc + 1) & "00", 2)         End If         s = s & dcStr         If Len(s) > 2 Then             MsgBox dcStr & "-  More Decimals"             s = ""         End If   But I am getting problem when entering more than 2 decimals or if I backspace the digits. Please Help me. ThanQ! Nov 22 '07 #2

 Expert 100+ P: 1,295 Also can I restrict to enter only 2 decimal places? I tried this code in TEXT_CHANGE() But I am getting problem when entering more than 2 decimals or if I backspace the digits. Please Help me. ThanQ! you can use doubles and singles instead of strings for this very problem Expand|Select|Wrap|Line Numbers dim dou1 as double dim sin1 as single dou1=abs(text1.text) sin1=dou1 - int(dou1) Depending on the version, you might or might not have ABS, if you dont, just create it Expand|Select|Wrap|Line Numbers public function abs( a as double) as double     if a < 0 then a = a * -1     abs = a end function If you want SIN1 as an integer, well, there are many ways: Expand|Select|Wrap|Line Numbers while int(sin1) <> sin1       sin1=sin1*10 wend or instead of doing that, to restrict it to N decimals just; Expand|Select|Wrap|Line Numbers sin1 = sin1 * 10^n HTH Nov 22 '07 #3

 100+ P: 115 you can use doubles and singles instead of strings for this very problem : : THANK YOU VERY VERY MUCH! But how to restrict i could not get it! ThanQ Once Again! Nov 22 '07 #4

 100+ P: 115 Hi Khadgar! I changed the code as Expand|Select|Wrap|Line Numbers     Dim dou1 As Double     Dim sin1 As Single     Dim n As Integer     dou1 = Abs(Text1.Text)     sin1 = dou1 - Int(dou1)   '    While Int(sin1) <> sin1 '     sin1 = sin1 * 10 'Wend       n = 2     sin1 = Round(sin1, n) * 10 ^ n       Text2.Text = sin1   ThaQ for an idea! Nov 22 '07 #5

 Expert 100+ P: 1,295 Hi Khadgar! I changed the code as ... ThaQ for an idea! yeap, that's right, the first method is to keep all decimals, the second to restrict them, you shouldnt use both, or you'll get all the decimals plus N zeros. Nov 22 '07 #6

 100+ P: 115 yeap, that's right, the first method is to keep all decimals, the second to restrict them, you shouldnt use both, or you'll get all the decimals plus N zeros. Yeah! I did the same. Thank You! Nov 23 '07 #7 