将 Python 浮点数转换为 C# 十进制

Convert Python floating-point into C# decimal

我正在尝试使用 pythonnet 将 Python 中的浮点数转换为 C# 中的 decimal


假设有一个数字“0.0234337688540165776476565071”。

我尝试了两种方法:

  1. 使用 float()
from System import Decimal
Decimal(0.0234337688540165776476565071)
# Same with Decimal(float(0.0234337688540165776476565071))

0.02343377
  1. 使用本机 Python 十进制
from System import Decimal
from decimal import Decimal as PyDecimal
Decimal(PyDecimal("0.0234337688540165776476565071"))

# This lose every number under the floating-point
0

我该怎么办...?

如果您将字符串转换为 python 浮点数,那么它将被截断为 64 位并且您将失去精度。您将不得不为 System.Decimal 使用 different constructor。例如:

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()

从您问题中的示例来看,您似乎正在尝试将字符串转换为 System.Decimal。为此,System.Decimal 有一个 Parse method:

from System import Decimal

Decimal.Parse("0.0234337688540165776476565071")

注意:根据您的情况,您可能还需要通过 CultureInfo.InvariantCulture