将整数转换为单个

时间:2014-03-25 16:55:19

标签: vb.net integer ctype

我正在使用我的.NET应用程序中名为“Aeonhack”的用户创建的Graph Control。添加时,比方说,一个大小为9的点,它必须是像9.0F一样的单身。

所以我想要一个将9之类的整数转换为像9.0F这样的整数的函数。

普通CType无效,我也尝试过:

Private Function IntToSingle(ByVal Number As Integer) As Single
    Return CType(Number & ".0F", Single)
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    GraphConnections.AddValue(IntToSingle(7))
End Sub

这给了我这个错误:

  

从字符串“7.0F”到“单一”类型的转换无效。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:6)

使用Convert.ToSingle

Convert.ToSingle(Number)

或只是CType,没有奇怪的字符串连接:

CType(Number, Single)

答案 1 :(得分:0)

您可以使用它转换为单个:CSng(9),但我认为这仅适用于VB.NET。

答案 2 :(得分:0)

I think the confusion is with 7.0F. That is not the same thing as the string "7.0F".

Per Microsoft

Appending the literal type character F to a literal forces it to the single data type.

You could simply use the F to force a double literal to be a single data type.

GraphConnections.AddValue(7.0F)

If, however, you receive an integer variable, then you would need to convert or cast it to a single as suggested by @MarcinJuraszek.

dim point As Integer = 7

GraphConnections.AddValue(Convert.ToSingle(point))