我正在尝试使用 pythonnet 将 Python 中的浮点数转换为 C# 中的
decimal
。
假设有一个数字
0.0234337688540165776476565071
。
我尝试过两种方法:
使用
float()
from System import Decimal
Decimal(0.0234337688540165776476565071)
# Same with Decimal(float(0.0234337688540165776476565071))
0.02343377
使用原生Python
Decimal
from System import Decimal
from decimal import Decimal as PyDecimal
Decimal(PyDecimal("0.0234337688540165776476565071"))
# This loses every number under the floating-point
0
我该怎么办?
从您问题中的示例来看,您似乎正在尝试将字符串转换为
System.Decimal
。为此,System.Decimal
有一个Parse
方法:
from System import Decimal
Decimal.Parse("0.0234337688540165776476565071")
注意:根据您的场景,您可能还需要通过
CultureInfo.InvariantCulture
。
如果将字符串转换为 python float,那么它将被截断为 64 位,并且您将失去精度。您必须为 System.Decimal
使用
不同的构造函数。例如:
public Decimal (int lo, int mid, int hi, bool isNegative, byte scale);
跳过验证,它可能看起来像这样
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()