Formatting a Integer in VB - vb.net

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)

Related

I have a problem converting decimal to hex in vb.net

I'm trying to convert 64bit decimal to hex but I get a wrong result
Public Function DecToBinary(dec As Double) As String
DecToBinary= Hex(dec)
End function
For dec=3689348814742970688 I get 0x3333333333436200 , I should get 0x3333333333436140
I will use the hex to get the binary data cause I couldn't find another way to get a string with the bits.
In the end 3689348814742970688 will become a string with bits
"0011001100110011001100110011001100110011010000110110000101000000"
Thanks in advance
Your assumption that dec contains 3689348814742970688 is wrong.
3689348814742970688 exceeds the precision range of a Double, so dec stores the "rounded" value of 3689348814742970880 instead.
To solve this issue, use a BigInteger from the System.Numerics namespace instead of a Double. It's .NET's data type for arbitrarily large integers.
' prints 3333333333436140
Console.WriteLine(BigInteger.Parse("3689348814742970688").ToString("x"))
This might work,
Dim l As Long = 3689348814742970688L
Dim s As String = Convert.ToString(l, 2)
Debug.WriteLine(s.ToString.PadLeft(64, "0"c))
l = Long.MaxValue
s = Convert.ToString(l, 2)
Debug.WriteLine(s.ToString.PadLeft(64, "0"c))

How do I convert integer to 4-digit hex string?

I need to convert a 2-byte signed integer into a string of it's hex equivalent, but I need the string to be 4 characters. I've tried the Hex() function but when I convert 0 or say, 10, the result is 0 or A. I need the result to be 0000 or 000A. Any tips or advice?
It is just
Dim hexS As String = i.ToString("X4")
This is well explained in the The Hexadecimal (X) Format specifier.
The 4 after the X specifies how many chars you want in the output and if there are not enough chars, the missing ones are supplied as an appropriate number of "0".
Since you also tagged the question VBA, here is a VBA way to do it
Right("0000" & Hex(i), 4)
Use
Dim i As Integer = 10
Dim hexS As String = i.ToString("X4")
If you are deadset on using Hex as opposed to string formatting then you could use:
Dim Number As Integer
Dim Output As String
Number = 10
Output = ("000" & Hex(Number))
Output = Output.Substring(Output.Length - 4, 4)
Console.WriteLine(Output)
Alternatively make use of string formatting for numbers as so:
Output = Number.ToString("X4")
Console.WriteLine(Output)
The output in both cases with be 000A.
The VB way
Format(i, "X4")
or
Hex(i).PadLeft(4, "0"c)
In Visual Studio 2015:
s = $"{i:X4}"

Find Nth number in a long variable in vbnet

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

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.

String.ToCharArray() stored in StringCollection conversion to numbers and back in VB.net

I have a string which was converted to a char array and stored in a stringcollection, how do I return the numerical value for the character in each string? How can I reverse this process, getting the character from the numerical value?
I know I could code a Select Case statement, but that would take a very long time as I need to cover every character a person could want to conceivably use in the English language, including punctuation.
Is there already a method built into vb.net for doing this?
Thanks for the help!
The Convert class has methods that can convert between characters and integers:
Dim c As Char = "A"C
Dim i As Integer = Convert.ToInt32(c)
Dim c2 As Char = Convert.ToChar(i)
To loop the values converted from the characters in a string into an array of integers:
Dim codes(theString.Length - 1) As Integer
For i As Integer = 0 to theString.Length - 1
codes(i) = Convert.ToInt32(theString.Chars(i))
Next
Try something like this:
For Each c As Char In yourString
Dim i As Int32 = DirectCast(c, Int32)
Next
Remeber that System.String implements IEnumerable<Char> so it is legal to For Each over it. And converting between a character and a number is as simple as casting between System.Char and System.Int32 (here I have shown how to get the numeric value for each character in the string).