首页 > 解决方案 > 将 Python 浮点数转换为 C# 十进制数

问题描述

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


假设有一个数字“0.0234337688540165776476565071”。

我尝试了两种方式:

  1. 使用浮动()
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

我该怎么办...?

标签: pythonc#decimalpython.net

解决方案


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

from System import Decimal

Decimal.Parse("0.0234337688540165776476565071")

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


推荐阅读