Welcome to Dream.In.Code
Become a VB Expert!

Join 137,228 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,918 people online right now. Registration is fast and FREE... Join Now!




separting numbers

 
Reply to this topicStart new topic

separting numbers

aNiXtEr
8 Feb, 2007 - 10:26 PM
Post #1

New D.I.C Head
Group Icon

Joined: 8 Feb, 2007
Posts: 25


My Contributions
So, I'm back. This question may show my ignorance, but I have to ask. How can I separate numbers for instance, if I had 123456789 and I wanted to perform calculations on just the 4 or 9 or whatever. How can I separate the numbers?
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Separting Numbers
9 Feb, 2007 - 12:33 AM
Post #2

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
Store the whole number in a string. Then use the Mid() function to extract each character (digit). Compare each result with the number you want to get. A loop will be required here.

Mid Function (Taken from MSDN)

Returns a Variant (String) containing a specified number of characters from a string.

Syntax

Mid(string, start[, length])

The Mid function syntax has these named arguments:

string Required. String expression from which characters are returned. If string contains Null, Null is returned.

start Required; Long. Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string ("").

length Optional; Variant (Long). Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.

This post has been edited by Louisda16th: 9 Feb, 2007 - 12:43 AM
User is offlineProfile CardPM
+Quote Post

aNiXtEr
RE: Separting Numbers
9 Feb, 2007 - 08:33 PM
Post #3

New D.I.C Head
Group Icon

Joined: 8 Feb, 2007
Posts: 25


My Contributions
I am validating data for a ISBN number. Here is my source code:
Public Class Form1
Dim producttotal As Integer

Dim s2 As String = 843610727
Dim s3 As Integer
'Public Sub calculateValues()
'End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = 84361072
Dim s1 = TextBox1.Text

'Dim value1, value2, value3 As Integer
'calculateValues()
'value1 = (TextBox1.Text)

'value1 = Val(s1(Mid(2, 1)))
Dim value1, value2, value3, value4, value5, value6, _
value7, value8, value9, value10 As Integer
Dim a1, a2, a3, a4, a5, a6, _
a7, a8, a9, a10 As String

value1 = Val(s1(Mid(0, 1)))
value2 = Val(s1(Mid(1, 1)))
value3 = Val(s1(Mid(2, 1)))
value4 = Val(s1(Mid(3, 1)))
value5 = Val(s1(Mid(4, 1)))
value6 = Val(s1(Mid(5, 1)))
value7 = Val(s1(Mid(6, 1)))
value8 = Val(s1(Mid(7, 1)))
'value9 = Val(s1(Mid(8, 1)))
'value10 = Val(s1(Mid(9, 1)))
a1 = value1 * 10
a2 = value2 * 9
a3 = value3 * 8
a4 = value4 * 7
a5 = value5 * 6
a6 = value6 * 5
a7 = value7 * 4
a8 = value8 * 3
'a9 = value9 * 2

producttotal = Convert.ToInt32(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8) '+ a9 '+ a10
TextBox2.Text = producttotal
End Sub
End Class

why does producttotal = 0?
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Separting Numbers
9 Feb, 2007 - 08:41 PM
Post #4

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
Who haven't declared producttotal. Is a string? If yes then you'll have to as
CODE

Dim ProductTotal As String

User is offlineProfile CardPM
+Quote Post

aNiXtEr
RE: Separting Numbers
9 Feb, 2007 - 08:46 PM
Post #5

New D.I.C Head
Group Icon

Joined: 8 Feb, 2007
Posts: 25


My Contributions
QUOTE(Louisda16th @ 9 Feb, 2007 - 09:41 PM) *

Who haven't declared producttotal. Is a string? If yes then you'll have to as
CODE

Dim ProductTotal As String



nevermind. Thanks for your help louisda16th.

This post has been edited by aNiXtEr: 9 Feb, 2007 - 09:54 PM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Separting Numbers
9 Feb, 2007 - 10:45 PM
Post #6

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
Oops! Sorry. Didn't see your code properly smile.gif But your code is giving an overflow here:
CODE

producttotal = Convert.ToInt32(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8) '+ a9 '+ a10

which does not occur when it is declared as a String. I don't think it makes any difference as far as output is concerned
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 02:55PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month