Is there a literal notation for decimal in IronPython?

Posted by jeroenh on Stack Overflow See other posts from Stack Overflow or by jeroenh
Published on 2010-04-09T08:07:59Z Indexed on 2010/04/09 8:13 UTC
Read the original article Hit count: 404

Filed under:
|
|
|
|

suppose I have the following IronPython script:

def Calculate(input):
    return input * 1.21

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

First, I could cast at the C# side: seems like a no-go as I might loose precision.

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

Second option is to use System.Decimal in the python script: works, but pollutes the script, which should be understandable for my users...

import clr
import System

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

Is there a way to tell the DLR that the number 1.21 should be interpreted as a Decimal, much like I would use the '1.21M' notation in C#?

© Stack Overflow or respective owner

Related posts about ironpython

Related posts about c#