Skip to content

Commit 062a0bc

Browse files
committed
Reflect PR #1 Support for Decimal
1 parent fb083bb commit 062a0bc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/runtime/converter.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
9191
if (op == doubleType)
9292
return Runtime.PyFloatType;
9393

94-
if (op == singleType)
94+
if (op == singleType || op == decimalType)
9595
return Runtime.PyFloatType;
9696

9797
if (op == boolType)
@@ -260,6 +260,17 @@ internal static IntPtr ToPython(object value, Type type)
260260
case TypeCode.UInt64:
261261
return Runtime.PyLong_FromUnsignedLongLong((ulong)value);
262262

263+
case TypeCode.Decimal:
264+
IntPtr mod = Runtime.PyImport_ImportModule("decimal");
265+
IntPtr ctor = Runtime.PyObject_GetAttrString(mod, "Decimal");
266+
267+
string d2s = ((decimal)value).ToString(nfi);
268+
IntPtr d2p = Runtime.PyString_FromString(d2s);
269+
IntPtr args = Runtime.PyTuple_New(1);
270+
Runtime.PyTuple_SetItem(args, 0, d2p);
271+
272+
return Runtime.PyObject_CallObject(ctor, args);
273+
263274
default:
264275
if (value is IEnumerable)
265276
{
@@ -752,6 +763,17 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
752763
result = num;
753764
return true;
754765
}
766+
case TypeCode.Decimal:
767+
op = Runtime.PyObject_Str(value);
768+
decimal m;
769+
string sm = Runtime.GetManagedString(op);
770+
if (!Decimal.TryParse(sm, NumberStyles.Number, nfi, out m))
771+
{
772+
goto type_error;
773+
}
774+
Runtime.XDecref(op);
775+
result = m;
776+
return true;
755777
default:
756778
goto type_error;
757779
}

0 commit comments

Comments
 (0)