简体   繁体   English

在vb.net中将十进制转换为空字符串

[英]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. 我敢肯定这很简单,但是我在努力完成这项工作,我尝试使用convert.tostring,decimal.tostring,ctype(object,type)和cstr(object),但没有成功。 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 . 您的变量是Decimal
It cannot hold a string. 它不能容纳字符串。

You need to declare a separate variable As String to hold the string. 您需要声明一个单独的变量As 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: 我不知道您为什么需要执行上述操作,但是我改用一个object

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... 尽管无法将十进制变量的值设置为String.Empty,但是您可以执行以下操作...

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. 您的问题是CSTR将值转换为字符串而不是对象本身,因此您要做的是获取一个十进制变量,然后将其值转换为字符串,然后尝试将字符串放回十进制。

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. 这是不可能的,因为0.0转换为字符串仍然是“ 0.0” string.Empty是“”,它只是一个空字符串。

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. 如果您的意思是如果要将值转换为0.0,则想将小数转换为字符串BUT,则可以使用IF语句轻松创建一个空字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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