Is there a decimal literal in IronPython?

Suppose I have the following IronPython script:

def Calculate(input):
    return input * 1.21

When called from C # with a decimal point, this function returns 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); 

I seem to have two options:

Firstly, I could dump on the C # side: it seems that no-go, as I may lose accuracy.

decimal result = (decimal)engine.Calculate(input); 

The second option is to use System.Decimal in a python script: it works, but it somewhat pollutes the script ...

from System import *

def CalculateVAT(amount):
    return amount * Decimal(1.21)

Is there a short-form DLR that the number 1.21 should be interpreted as decimal, just as I would use the notation “1.21m” in C #? Or is there another way to force a decimal instead of two?

+5
2

python:

, . float, . ( ).

, , . python . " " :

from decimal import Decimal
my_decimal = Decimal("1.21")

, .net- (, ), .net , . , . , :

from System import *
my_decimal = Decimal(1.21)

, :

from System import *
my_decimal = Decimal(121, 0, 0, False, 2)

, , .

+4

, .

, :

Decimal(1.21)

, 1.21 , , Decimal. Python native Decimal , :

decimal.Decimal('1.21')

, .NET Decimal, , :

System.Decimal(121, 0, 0, False, 2)
+3

All Articles