I have 532.016, and I want to get only the 532 part in VB.NET. How can I do that?
Math.Truncate(myDecimal)
will strip away the fractional part, leaving only the integral part (while not altering the type; i. e. this will return the type of the argument as well, be it Double or Decimal).
Cast it to an Integer.
Dim myDec As Decimal
myDecimal = 532.016
Dim i As Integer = Cint(myDecimal)
'i now contains 532
You could use System.Text.RegularExpressions:
Imports System.Text.RegularExpressions 'You need this for "Split"'
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourNumber As String = 532.016 'You could write your integer as a string or convert it to a string'
Dim whatYouWant() As String 'Notice the "(" and ")", these are like an array'
whatYouWant = Split(yourNumber, ".") 'Split by decimal'
MsgBox(whatYouWant(0)) 'The number you wanted is before the first decimal, so you want array "(0)", if wanted the number after the decimal the you would write "(1)"'
End Sub
End Class
Decimal.floor(532.016) will also return 532.
Decimal.Floor rounds down to the closest integer.
However it won't work for negative numbers. See this Stack Overflow question for a complete explanation
Decimal.Truncate (or Math.Truncate) is really the best choice in your case.
Related
I try to run this code but iget the message implicit conversion from double to string vb.net i try different conversion but its the same
Private Sub TxtRemise_TextChanged(sender As Object, e As EventArgs) Handles TxtRemise.TextChanged
Dim TotalTTc As Decimal
TotalTTc = CDec(TotalHT.Text) + CDec(TXTMontantTva.Text)
TxtTTC.Clear()
TxtTTC.Text = TotalTTc) - val(TxtRemise.Text)
End Sub
You perform a mathematical operation, which will obviously result in a number, and you then assign that to the Text property of a TextBox, which is type String. If you want to assign to a String property then you need to assign a String. If you have a number, that means calling ToString on it.
I checked your code. There was some little mistakes. Check my example and comment if you had the solution to your question, thank you.
Public Class Form1
Private Sub TxtRemise_TextChanged(sender As Object, e As EventArgs) Handles TxtRemise.TextChanged
Dim TotalTTc As Decimal
TotalTTc = CDec(TotalHT.Text) + CDec(TXTMontantTva.Text)
TxtTTC.Clear()
TxtTTC.Text = (TotalTTc - CDec(TxtRemise.Text)).ToString
End Sub
End Class
In the function below, what is the meaning of the & character? I'm not aware how it's valid syntax or how this would be used normally.
Normally using &= together would do a concatenation and assignment, resulting in "Hello world". However in this "typo" the code actually compiles but the result is just an assignment of " world" to the MyStr variable. Also if you define MyStr in the function rather than at the class level, the result is that it will not compile and interprets the & as a Type Character and fails because MyStr is a string and not of type Long.
Public Class MyClassName
Private MyStr As String
Public Sub New()
End Sub
Public Function MyFunction() As String
MyStr = "Hello"
MyStr& = " World"
Return MyStr
End Function
End Class
Microsoft admits it's a bug in the compiler.
It is not going to be fixed because that would be a breaking change.
As you mention, the & is acting as a type character. Type characters are essentially just a shorthand for declaring the type of a variable. For example, you could use
dim value& = 100 rather than dim value as Long = 100
They are generally used when defining a value on the fly, such as when passing a value to a function:
dim result = funcThatTakesALong(100&)
You can also see in the link that you posted there are also type characters for integers (%), decimals (#), singles (!), doubles (#), and strings ($).
Edit: formatting
I think this may explain the purpose of the operators
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
XYZ(9&) ' Long values
XYZ(9L)
XYZ(9I) ' Int32 values
XYZ(9%)
XYZ(9S) 'Int16 value
XYZ("9"c) ' a single charcter
XYZ("9") ' a string
End Sub
Sub XYZ(ByVal arg As Object)
Debug.Print("arg type is " & arg.GetType.ToString)
'do some stuff depends on the type of arg
End Sub
.
I'm porting some code from php to vb.net, but I stuck
on the XOR function. I cannot perform the XOR when one of the
values is greater than integer datatype range.
Here is my testing code :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim A As Integer = 1254378503
Dim B As Integer = 3058210502 'Overflow here
Dim SIGN1 = A Xor B
Dim SIGN2 = EncodeBase64(SIGN1)
MsgBox(SIGN2)
End Sub
Public Function EncodeBase64(ByVal input As String) As String
Return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(input))
End Function
End Class
I've been browsing through other posts regarding my question, but honestly
I cannot find any solution or simply cannot understand it. The basic question is how do I perform XOR on decimal datatype (greater then integer) just like php does.
Any help or hint on how to achieve it will be much apreciated.
Simple answer. Use Long instead of Integer. Basically, the Integer type is too small to hold the values you are using in your sample.
The output of your test code is NDIzNzA4OTQ3Mw== - coded through VS Express, just to show that this actually works.
Dim numbers(9) As Integer
Dim Card As String
Dim CardInfo As Integer
Const ListSize = 9
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
listOutput.Items.Clear()
'populate the numbers array
For i As Integer = 0 To 9
numbers(i) = txtValue.Text
Card = txtCard.Text
CardInfo = numbers & Card
listOutput.Items.Add(CardInfo)
Next
For i = 0 To ListSize
Next i
txtSearchValue.Focus()
End Sub
Your problem is here:
numbers(i) = txtValue.Text
Numbers is an array of integer and txtValue.Textis a String.
You cannot save a String into an integer. My guess is that you want the value of that string. If its a "23" you want the number 23. For that you need to parse.
numbers(i) = Integer.Parse(txtValue.Text)
That will save the value of your string into the int.
Important:
If txtValue have a not valid text, something that's not a number it will throw an exeption. And we don't want that. So we can use TryParse:
Integer.TryParse(txtValue.Text,numbers(i))
If you are sure that it will always have a valid number on the txtValue, you can use the standart Parse, if not, use TryParse
First of all, it's not complaining about a String + Integer. It's complaining about String + Integer(). The numbers variable used with the & operator in that code is an array, which doesn't just concatenate with strings. The subscript is missing.
But you can greatly reduce all that code anyway:
Const ListSize = 9
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim items = Enumerable.Range(0, ListSize).Select(Function (i) String.Format("{0} {1}", txtCard.Text, i)).ToArray()
listOutput.Items.Clear()
listOutput.Items.AddRange(items)
txtSearchValue.Focus()
End Sub
Dim suffix As String = "_version"
I have a file called "Something_version1.jpg"
I need to split this so that I get "Something_version"
The following gets me "1.jpg"
filename = filename.Split(New String() {suffix}, StringSplitOptions.None)(1)
And the following gets me "Something"
filename = filename.Split(New String() {suffix}, StringSplitOptions.None)(0)
But what I need is "Something_version"
The suffix is dynamic, and can change.
Hope this is easier than I'm making it.
Thank you.
If you don't care about the "1.jpg" part at all, and all you want is the suffix and the part before the suffix, you can do what you have above (the second one) to get the prefix, and simply concatenate the prefix and suffix to get the answer you're looking for.
The split call might be overkill but it will do the job.
Try this!
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim suffix As String = "_version"
Dim searchThis As String = "something_version1.png"
MsgBox(GetPrefix(suffix, searchThis))
End Sub
Function GetPrefix(suffix As String, searchThis As String) As String
Dim suffixLocation As Integer = searchThis.IndexOf(suffix) + suffix.Count
Return searchThis.Substring(0, suffixLocation)
End Function
End Class