简体   繁体   中英

Converting a decimal to an empty string in vb.net

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.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM