Find Nth number in a long variable in vbnet - vb.net

I have a very long number that goes like this:
numb as long=011212201220200112202001200101121220200120010112200101120112122....
It will be more than 4,000,000,000 digits. My problem is to find any digit in the number. If it was integer I would convert to string and do this:
numb(200)
But his is Long. Do you know how to find this?

As with integers, you can convert a long into a string too, in order to get the n-th element
Dim numb As Long = 9876543210
Dim targetDigit As Integer = 3 ' Set target as the 3rd digit
numb.ToString()(targetDigit -1) ' Retuns the 3rd digit: 7
Side note: I suspect that you may know this but, a long datatype can only hold
Integers ranging in value from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
That's only 19 digits! No where near 4 billion.
Source: MSDN

Related

Incrementing 5 digit number, including leading zeros

I need to generate statement numbers in Access 2016. These numbers need to be 5 digits long, and where the statement number is less than 5 digits, I need to pad the number with leading zeros.
As a starting point, the first statement number will not be padded. So if the last statement number is 96, the next statement number needs to be 00097.
Is it possible (I assume in VBA) to cater for both cases, where the number is 5 digits long, or where it is less.
I was thinking of firstly stripping the leading zeros from the last statement number (if any existed), incrementing the remaining number by one, and then adding leading zeros to make the length of the number 5, but I'm not sure of the most efficient way of doing this.
If it has a leading zero, it will not be a number, but a String. However, as far as it is not quite a bit difference, something like this works:
Sub TestMe()
Dim cnt As Long
Dim myString As String
For cnt = 98 To 120
myString = Format(cnt, "00000")
Debug.Print myString
Next cnt
End Sub
This is what you get in the immediate window:
00098
00099
00100
00101
Yes, just first convert to a number:
NextValue = Format(Val(CurrentValue) + 1, "00000")
You could also use:
NextStateStr = Right("00000" & LastStateNum + 1, 5)

getting overflow error, when I used the function "ActiveSheet.UsedRange.Rows.Count" to find the count in excel VBA

I am getting an overflow error, when I tried to use the function
ActiveSheet.UsedRange.Rows.Count
in VBA Excel
I am going on assumption you have something like the following:
Dim x as Integer
x = ActiveSheet.UsedRange.Rows.Count
To fix simply change it to:
Dim x as Long
x = ActiveSheet.UsedRange.Rows.Count
Or
Dim x&
x = ActiveSheet.UsedRange.Rows.Count
The ampersand (&) type-declaration character represents a Long
The Integer and Long data types can both hold positive or negative values. The difference between them is their size: Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647
Because Excel worksheets can have more then 32,767 rows, you cannot ALWAYS fit the number of rows into an integer, and must use a Long.
Have you defined your variable as an integer rather than a long or float? If there are more than 32256 or something rows it's too big to be an integer.

how to i create a Username shortener?

I have an AD username called "lastname-132" in Textbox1, this string is 12 long, so i want to add the username in to Textbox2, but shortened, in the textbox2 i only have a string length of only 10 available due to other tools this program is using, so i don't want to convert it all the time manually and want to just convert it automatically with a onleave event.
Anyone any idea how to write this?
So the End Result should look like this.
'String length can be 20 max.
Textbox1.Text = "lastname-123"
'some code to convert it to this:
'String length 10 max. Numbers and the "-" should stay the same, but remove letters if necessary.
Textbox2.Text = "lastna-123"
Here's the concept:
Split string based on '-' into 2 strings
In the example above: 'lastname' and '123'.
Check the length of the first string and cut if it is too long
the program checks 'lastname' and finds that it is too long, then
cuts it into 'lastna'
Combine 'lastna' and '123' back into a string
I hope this helps
Without more information, this will assume that there can be multiple hyphens, the number can be of variable length, and you can change the maximum length of the string by changing one variable.
Dim username As String = "lastname-123"
Dim max As Integer = 10
Dim lindex As Integer = username.LastIndexOf("-")
Dim numberLength As Integer = username.Length - lindex
Dim number As String = username.Substring(lindex)
Dim justName As String = username.Substring(0, lindex)
If justName.Length + numberLength >= max Then
username = justName.Substring(0, max - numberLength) & number
End If
If you are concentrating only on the restriction of length of characters to be accepted then you can use
Maxlength
property of the Textbox.
Ex: Maxlength="10"
restricts the Textbox to accept only 10 characters.
Try to make it fit with for example substring manipulation. See http://msdn.microsoft.com/en-us/library/dd789093.aspx for more info.

Formatting a Integer in VB

How can I format a integer, 5500000.00 to something like 5,50 Mil.?
How do i convert it or format it?
Thank You.
Yes, you need to convert it to a string to display it that way, but you don't have to convert it to a string to actually get the numeric number of millions. For instance:
Dim total As Integer = 5500000
Dim millions As Decimal = total / 1000000
Dim formatted As String = String.Format("{0} Mil.", millions)
Yes.
You probably don't realize the difference between integer, and it's display (in this case probably decimal) value. 5500000 is string. If you convert it to integer, it is stored somewhere in memory as 00000000 01010011 11101100 01100000 (in bits).
The display value, 5500000, 55.0 Mil are both strings. Computer doesn't know that 5500000 is number - it can only parse the text to it's numerical representation.
Dim value As Integer = 5500000
Dim valueInMil As Single = (CType(value,Single) / 1000000)

Best way to get the first digit from an integer of varying length in VB.NET

I am a newbie to programming and need some help with the basics.
I have a function which takes in an integer value. I want to be able to grab the first digit (or the first and second digits in some cases) of this integer and do something with it.
What is the best way in VB.NET to get the first digit of an integer (or the first and second)?
firstDigit = number.ToString().Substring(0,1)
firstTwoDigits = number.ToString().Substring(0,2);
int.Parse(firstDigit)
int.Parse(firstTwoDigits)
and so forth
I'm not well versed in VB syntax, so forgive me for the syntax errors:
dim i as integer
while i >= 10
i = i \ 10
end while
msgbox "i = " & i
Note, this prints the "first from the left" digit. Like, for "12345" it would print "1".
If you need the digits starting from the end of the integer, just get the modulu result for the tens or the hundreds, according to how many digits you need.
Dim n As Integer
n Mod 10
for the first digit, or:
n Mod 100
for the second and first digits.
If you need the first and second digits from the beginning of the number, there is another answer here which will probably help you.
for first digit you can use:
Dim number As Integer = 234734
Dim first = number.ToString.ToCharArray()(0)
for second digit you can use:
Dim number As Integer = 234734
Dim second = number.ToString.ToCharArray()(1)
This would work. You can use Math.ABS, absolute value, to eliminate negative. The number from left could be replaced by a function if you are using logic, like the overall length of the number, to determine how many of the leading characters you are going to use.
Dim number As Integer = -107
Dim result As String
Dim numberFromLeft As Integer = 2
result = Math.Abs(number).ToString.Substring(0, numberFromLeft)
This results in 10 it is a string but converting it back to a number is easy if you need to. If you need to keep track if it was positive or negative you could use the original value to apply that back to you parsed string.