I'm sure this is simple to do but I'm struggling to get this work, I've tried using convert.tostring, decimal.tostring, ctype(object, type) and cstr(object) but without success. I guess I'm trying to change the decial object to a string object and then assign it a value of empty string but always get type mismatch error.
Dim testdecimal as decimal = 0.0
testdecimal = Cstr(testdecimal)
testdecimal = string.empty
Your variable is a Decimal.
It cannot hold a string.
You need to declare a separate variable As String to hold the string.
Although you can't set the value of a decimal variable to String.Empty, you can do something like this...
Dim TestDecimal As Decimal = 0.0
Dim strStringValue As String = IIf(TestDecimal = 0.0, "", TestDecimal.ToString())
MsgBox(strStringValue)
You can't convert a decimal to an empty string. I don't know why you would need to do the above, but I would use an object instead:
Dim test As Object = 10.12345
test = "Hi"
test = String.Empty;
It looks like you just need to create a string representiation of the number, which you can do like this:
'create a decimal variable
Dim testDec As Decimal = 10.12345
'convert decimal to string and then set to empty string
Dim testStr As String = testDec.ToString("N")
testStr = String.Empty
Your problem here is that CSTR converts the value to string and not the object itself so what you are doing is taking a decimal variable then converting its value to string and trying to put the string back into the decimal.
Dim testdecimal as decimal = 0.0 'testDecimal is a decimal type and you are assigning a decimal value
testdecimal = Cstr(testdecimal) 'testDecimal is still a decimal but you are trying to put a string in it Here is your first type mismatch
testdecimal = string.empty ' If this actually had worked it would have made the above line pointless because you just tried to overwrite the value (even though this line did not execute here is your second type mismatch)
What you would need to do is:
Dim NewString as String
Dim testdecimal as decimal = 0.0
NewString = Cstr(testdecimal)
The above takes the decimal value and converts it to string then stores it into a string variable.
Now for the second part of your problem, converting a decimal to an empty string. This is impossible because 0.0 converted to string is still "0.0" string.Empty is "" its just an empty string.
If what you mean is you want to convert a decimal to string BUT if the value is 0.0 then make an empty string you can easily do that with an IF statement.
Basically just do:
Dim NewString as String
Dim testdecimal as decimal = 0.0
if(testdecimal =0.0) Then
NewString = String.Empty
Else
NewString = Cstr(testdecimal)
END IF
Related
Example: The float value is -1580.719 and I need to convert it to hex in VB.NET (Value in hex should output: C4C59704).
Ok, so I found how to do it:
Dim var As Single = Single.Parse("-1580.719")
Dim varArray() As Byte = BitConverter.GetBytes(var)
Array.Reverse(varArray)
Dim result As String = BitConverter.ToString(varArray).Replace("-", "")
Value of result is:
C4C59702
Const gconintRows1st As Integer = 15
Const gconintRows4th As Integer = 20
I am trying to convert String to Integer
by using:
intNumber = CInt(strNumberSelected(intFirst5Balls))
Professor's
intTemperature = CInt(strTemperatures(intMonth))
But some how this does not work.
The professor's version worked fine but I cannot figure out why the intNumber does not.
Yes, this is my first time doing vb
It looks like strNumberSelected is a string array, correct? And you're asking for the string in the array at position intFirst5Balls?
Take this for an example:
Dim strArray As String() = Split("Hi|there|everybody", "|")
The Split() function will split our long string at each occurrence of a pipe ("|").
The resulting string array will have the following 3 elements:
"Hi"
"there"
"everybody"
And you'd reference these elements by their indexes:
strArray(0) = "Hi"
strArray(1) = "there"
strArray(2) = "everybody"
If you're getting the error 'Char' values cannot be converted to 'Integer' then your array is more likely an array of type Char, and the CInt() function cannot convert it to an integer.
You can use
Integer.TryParse()
to try to get a valid Integer from your array, but it sounds a bit like you're unsure what sort of data actually lives in the array.
I have to convert number (double) to string like this:
Dim myDouble = 3.14
Dim myDoubleStr = myDouble.ToString ''OR myDouble.ToString("N")
According to my 'culture' settings result is "3,14" what is in most cases OK.
But here are cases that I need string representation of a number with decimal point instead of comma.
In that case I replace char "," with "." like string manipulation.
Is here a way that "ToString" convert a number with decimal point directly when this is needed?
Try
.ToString("F", CultureInfo.InvariantCulture)
More info here
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
You can also have as much precession as you want by specifying the format like this:
Dim myDouble As Double = 3.14159268
Dim myDoubleStr = myDouble.ToString("0.00000") 'The value will be 3.14159
In case you wanted to use Thousands separator, use this format:
Dim myDouble = 961327.1234567890
Dim MyDoubleStr = myDouble.ToString("#,##0.00000")
'The value of MyDoubleStr will be 961,327.12345
Example 1
Dim myStr As String = "38"
I want my result to be 38.000 ...
Example 2
myStr = "6.4"
I want my result to be 6.400
What is the best method to achieve this? I want to format a string variable with atleast three decimal places.
Use FormatNumber:
Dim myStr As String = "38"
MsgBox(FormatNumber(CDbl(myStr), 3))
Dim myStr2 As String = "6.4"
MsgBox(FormatNumber(CDbl(myStr2), 3))
So if you have
Dim thirtyEight = "38"
Dim sixPointFour = "6.4"
Then, the best way to parse those to a numeric type is, Double.Parse or Int32.Parse, you should keep your data typed until you want to display it to the user.
Then, if you want to format a string with 3 decimal places, do somthing like String.Format("{0:N3}", value).
So, if you want a quick hack for the problem,
Dim yourString = String.Format("{0:N3}", Double.Parse("38"))
would do.
Take a look on "Standard Numeric Format Strings"
float value = 6.4f;
Console.WriteLine(value.ToString("N3", CultureInfo.InvariantCulture));
// Displays 6.400
In pseudo code
decpoint = Value.IndexOf(".");
If decpoint < 0
return String.Concat(value,".000")
else
return value.PadRight(3 - (value.length - decpoint),"0")
If it's string keep it as a string. If it's a number pass it as one.
I have a string in VB.net that may contain something like the following:
This is a 0x000020AC symbol
This is the UTF-32 encoding for the Euro Symbol according to this article http://www.fileformat.info/info/unicode/char/20ac/index.htm
I'd like to convert this into
This is a € symbol
I've tried using UnicodeEncoding() class in VB.net (Framework 2.0, as I'm modifying a legacy application)
When I use this class to encode, and then decode I still get back the original string.
I expected that the UnicodeEncoding would recognise the already encoded part and not encode it against. But it appears to not be the case.
I'm a little lost now as to how I can convert a mixed encoded string into a normal string.
Background: When saving an Excel spreadsheet as CSV, anything outside of the ascii range gets converted to ?. So my idea is that if I can get my client to search/replace a few characters, such as the Euro symbol, into an encoded string such as 0x000020AC. Then I was hoping to convert those encoded parts back into the real symbols before I insert to a SQL database.
I've tried a function such as
Public Function Decode(ByVal s As String) As String
Dim uni As New UnicodeEncoding()
Dim encodedBytes As Byte() = uni.GetBytes(s)
Dim output As String = ""
output = uni.GetString(encodedBytes)
Return output
End Function
Which was based on the examples on the MSDN at http://msdn.microsoft.com/en-us/library/system.text.unicodeencoding.aspx
It could be that I have a complete mis-understanding of how this works in VB.net. In C# I can simply use escaped characters such as "\u20AC". But no such thing exists in VB.net.
Based on advice from Heinzi I implemented a Regex.Replace method using the following code, this appear to work for my examples.
Public Function Decode(ByVal s As String) As String
Dim output As String = ""
Dim sRegex As String = "0x[0-9a-zA-Z]{8}"
Dim r As Regex = New Regex(sRegex)
Dim myEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf HexToString)
output = r.Replace(s, myEvaluator)
Return output
End Function
Public Function HexToString(ByVal hexString As Match) As String
Dim uni As New UnicodeEncoding(True, True)
Dim input As String = hexString.ToString
input = input.Substring(2)
input = input.TrimStart("0"c)
Dim output As String
Dim length As Integer = input.Length
Dim upperBound As Integer = length \ 2
If length Mod 2 = 0 Then
upperBound -= 1
Else
input = "0" & input
End If
Dim bytes(upperBound) As Byte
For i As Integer = 0 To upperBound
bytes(i) = Convert.ToByte(input.Substring(i * 2, 2), 16)
Next
output = uni.GetString(bytes)
Return output
End Function
Have you tried:
Public Function Decode(Byval Coded as string) as string
Return StrConv(Coded, vbUnicode)
End Function
Also, your function is invalid. It takes s as an argument, does a load of stuff and then outputs the s that was put into it instead of the stuff that was processed within it.