你这里的问题是,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)
什么,你需要做的是:
Dim NewString as String
Dim testdecimal as decimal = 0.0
NewString = Cstr(testdecimal)
以上采用十进制值,并将其存储将其转换为字符串,然后进入一个字符串变量。
现在针对问题的第二部分,将小数转换为空字符串。这是不可能的,因为0.0转换为字符串仍然是“0.0”string.Empty是“”它只是一个空字符串。
如果你的意思是你要将十进制转换为字符串,但如果值为0.0,那么创建一个空字符串,你可以很容易地用IF语句来做到这一点。
基本上只是做:
Dim NewString as String
Dim testdecimal as decimal = 0.0
if(testdecimal =0.0) Then
NewString = String.Empty
Else
NewString = Cstr(testdecimal)
END IF
这是C#,这是一个坏主意。 – SLaks
将其更改为VB。没有说这是一个好主意 - 只是向OP展示了需要完成的任务。我包含了更好的代码替代方案。 –
为什么要把小数转换为字符串然后覆盖它呢?为什么不只是昏暗testStr作为字符串= String.Empty –