If you cast a string to a python float then it will be truncated to 64-bit and you will lose precision. You will have to use a different constructor for System.Decimal. For instance:
public Decimal (int lo, int mid, int hi, bool isNegative, byte scale);
Skipping validation, it might look something like
from System import Decimal
from System import Int32, Boolean, Byte
def str_to_csp_decimal(number: str) -> Decimal:
""" convert a string to a C# Decimal """
is_negative = number[0] == "-"
abs_value = number[1:] if is_negative else number
has_fraction = "." in abs_value
if has_fraction:
integer, fraction = abs_value.split(".")
scale = len(fraction) # denominator = 10 ** scale
numerator = int(integer + fraction) # no precision loss for integers
else:
scale = 0 # denominator == 1
numerator = int(abs_value)
assert numerator < (2 << 96), "Decimal only has 96 bits of precision"
# split the numerator into the lower, mid, and high 32 bits
mask = 0xFFFFFFFF
low = Int32(numerator & mask)
mid = Int32((numerator >> 32) & mask)
high = Int32((numerator >> 64) & mask)
return Decimal(low, mid, high, Boolean(is_negative), Byte(scale))
str_to_csp_decimal("0.0234337688540165776476565071").ToString()
PyDecimalabove is native Python type, and I'm trying to convert Python'sDecimalintoSystem.Decimalof C#. That's why I didDecimal(PyDecimal(0.212....)).