次の IronPython スクリプトがあるとします。
def Calculate(input):
return input * 1.21
10 進数で C# から呼び出されると、この関数は double を返します。
var python = Python.CreateRuntime();
dynamic engine = python.UseFile("mypythonscript.py")
decimal input = 100m; // input is of type Decimal
// next line throws RuntimeBinderException:
// "cannot implicitly convert double to decimal"
decimal result = engine.Calculate(input);
私には2つのオプションがあるようです:
まず、C# 側でキャストすることができました。精度が失われる可能性があるため、うまくいかないようです。
decimal result = (decimal)engine.Calculate(input);
2 番目のオプションは、Python スクリプトで System.Decimal を使用することです。動作しますが、スクリプトが多少汚染されます...
from System import *
def CalculateVAT(amount):
return amount * Decimal(1.21)
C#で「1.21m」表記を使用するのと同じように、数値1.21を10進数として解釈する必要がある省略表記DLRはありますか?または、double の代わりに使用する 10 進数を強制する他の方法はありますか?