469,909 Members | 1,683 Online

# How to get Decimal Points in Numeric Value

115 100+
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.

Expand|Select|Wrap|Line Numbers
1.  VB6
2.         Dim x As Double
3.         Dim dc As Integer, dcStr As String
4.
5.         x = Val(Text1.Text)
6.         dc = InStr(x, ".")
7.         If dc > 0 Then
8.             dcStr = Left(Mid(Text1.Text, dc + 1) & "00", 2)
9.         End If
10.
ThnQ all !
Nov 22 '07 #1
6 15359
Mohan Krishna
115 100+
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.

:
:
ThnQ all !
Also can I restrict to enter only 2 decimal places?
I tried this code in TEXT_CHANGE()
Expand|Select|Wrap|Line Numbers
1.         Dim x As Double
2.         Dim dc As Integer, dcStr As String
3.
4.         x = Val(Text1.Text)
5.         dc = InStr(x, ".")
6.         If dc > 0 Then
7.             dcStr = Left(Mid(Text1.Text, dc + 1) & "00", 2)
8.         End If
9.         s = s & dcStr
10.         If Len(s) > 2 Then
11.             MsgBox dcStr & "-  More Decimals"
12.             s = ""
13.         End If
14.
But I am getting problem when entering more than 2 decimals or if I backspace the digits.
ThanQ!
Nov 22 '07 #2
1,295 Expert 1GB
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.
ThanQ!
you can use doubles and singles instead of strings for this very problem

Expand|Select|Wrap|Line Numbers
1. dim dou1 as double
2. dim sin1 as single
3. dou1=abs(text1.text)
4. 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
1. public function abs( a as double) as double
2.     if a < 0 then a = a * -1
3.     abs = a
4. end function
If you want SIN1 as an integer, well, there are many ways:

Expand|Select|Wrap|Line Numbers
1. while int(sin1) <> sin1
2.      sin1=sin1*10
3. wend
or instead of doing that, to restrict it to N decimals just;

Expand|Select|Wrap|Line Numbers
1. sin1 = sin1 * 10^n
HTH
Nov 22 '07 #3
Mohan Krishna
115 100+
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
Mohan Krishna
115 100+

I changed the code as
Expand|Select|Wrap|Line Numbers
1.     Dim dou1 As Double
2.     Dim sin1 As Single
3.     Dim n As Integer
4.     dou1 = Abs(Text1.Text)
5.     sin1 = dou1 - Int(dou1)
6.
7. '    While Int(sin1) <> sin1
8. '     sin1 = sin1 * 10
9. 'Wend
10.
11.     n = 2
12.     sin1 = Round(sin1, n) * 10 ^ n
13.
14.     Text2.Text = sin1
15.
ThaQ for an idea!
Nov 22 '07 #5
1,295 Expert 1GB

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
Mohan Krishna
115 100+
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