Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
十进制转换F#脚本与编译F#_F#_Decimal_Type Conversion - Fatal编程技术网

十进制转换F#脚本与编译F#

十进制转换F#脚本与编译F#,f#,decimal,type-conversion,F#,Decimal,Type Conversion,在F#Interactive中,以下代码起作用: > printfn "%A" (decimal 1I) 1M 但是,在已编译的F#程序中,会出现一条错误消息: The type 'Numerics.BigInteger' does not support a conversion to the type 'decimal' 那里发生了什么事?是因为F#版本之间使用了不同的引用集(以及引用版本)吗?或者,十进制的内部表示形式在编译模式和解释模式之间是不同的。这可能是因为您的F#编译程序

在F#Interactive中,以下代码起作用:

> printfn "%A" (decimal 1I)
1M
但是,在已编译的F#程序中,会出现一条错误消息:

The type 'Numerics.BigInteger' does not support a conversion to the type 'decimal'

那里发生了什么事?是因为F#版本之间使用了不同的引用集(以及引用版本)吗?或者,十进制的内部表示形式在编译模式和解释模式之间是不同的。

这可能是因为您的F#编译程序的目标是.NET Framework 2.0/F#2.0。F#interactive使用.NET Framework 4.0/F#4.0

2.0框架使用FSharp.Core中的
biginger
。4.0框架使用
System.Numerics.biginger
。FSharp.Core one没有到十进制的转换


将您的项目更改为target.NET 4.0,并添加对System.Numerics和所有内容的引用。

您是对的,是否可以使用
decimal
函数转换
BigInteger
存在一些不一致。这似乎取决于编译的.NET版本。如果您使用的是Visual Studio 2010中的F#编译器(或F#interactive),那么默认目标是.NET 4.0。为了实现这一目标,汇编工作做得很好:

C:\Temp>"C:\Program Files (x86)\Microsoft F#\v4.0\Fsc.exe" test.fs
Microsoft (R) F# 3.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
您可以通过显式引用.NET 2.0版本的
mscorlib.dll
FSharp.Core.dll
来更改目标框架。然后编译器报告您描述的错误:

C:\Temp>"C:\Program Files (x86)\Microsoft F#\v4.0\Fsc.exe" test.fs --noframework 
  -r:C:\Program Files (x86)\FSharp-2.0.0.0\bin\FSharp.Core.dll 
  -r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll
Microsoft (R) F# 3.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

test.fs(1,23): error FS0001: The type 'System.Numerics.BigInteger' does not support 
  a conversion to the type 'decimal'

如果在编译项目时出现错误,则您的项目可能已配置为针对.NET 2.0进行编译。

相同的结果

Microsoft(R) F# 2.0 Interactive ビルド 4.0.40219.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
> printfn "%A" (decimal 1I);;
1M
val it : unit = ()

>fsc test.fs
Microsoft(R) F# 2.0 Compiler ビルド 4.0.40219.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

>test
1M

+你赢了我。我将把我的答案留在那里,以防其他一些细节有用。