将int转换为F#中的float

Nit*_*xDM 22 f#

学习F#,语法对我来说仍然很陌生.如何将此整数转换为float?

let add x y =
    x + y

let j = 2
add 1.1 j
Run Code Online (Sandbox Code Playgroud)

在C#Float + int = Float中

float j = 1.1f + 5;
Run Code Online (Sandbox Code Playgroud)

fer*_*lin 27

编辑:误读问题......

我很确定该float()功能可以完成这项工作:

add 1.1 (float 2)
Run Code Online (Sandbox Code Playgroud)


Wes*_*ser 9

首先,您指定的函数具有类型int->int->int,这意味着它需要2 int秒并返回一个int.如果您希望它使用floats,您需要指定其中一个参数的类型:

let add (x : float) y = x + y
//add : float->float->float
Run Code Online (Sandbox Code Playgroud)

正如其他人所提到的,你可以float使用float()函数强制转换为:

float 2 //2 : float
Run Code Online (Sandbox Code Playgroud)

如果您在示例中使用数字文字,则可以使用2.0而不是2具有该float类型的数字文字.

add 1.1 2.0
Run Code Online (Sandbox Code Playgroud)


Lee*_*Lee 5

您可以使用以下float函数转换为float :

let add x y = x + (float y)
Run Code Online (Sandbox Code Playgroud)