python - 将 Python 浮点数转换为 C# 十进制数
问题描述
decimal
我正在尝试使用 pythonnet将 Python 中的浮点数转换为C# 中的浮点数。
假设有一个数字“0.0234337688540165776476565071”。
我尝试了两种方式:
- 使用浮动()
from System import Decimal
Decimal(0.0234337688540165776476565071)
# Same with Decimal(float(0.0234337688540165776476565071))
0.02343377
- 使用原生 Python 十进制
from System import Decimal
from decimal import Decimal as PyDecimal
Decimal(PyDecimal("0.0234337688540165776476565071"))
# This lose every number under the floating-point
0
我该怎么办...?
解决方案
从您问题中的示例来看,您似乎正在尝试将字符串转换为System.Decimal
. 为此,System.Decimal
有一个Parse
方法:
from System import Decimal
Decimal.Parse("0.0234337688540165776476565071")
CultureInfo.InvariantCulture
注意:根据您的情况,您可能还需要通过。
推荐阅读
- java - 我的应用程序非常大(10GB)。如何查看数据或错误代码?
- c# - WPF(MVVM) 表“游戏技能序列”。列中只有一个选择
- python - 函数 replace_list(list_1, value1, value2) 其中 value1 必须替换为 list_1 中的 value2
- perl - perl : 不能从其他 perl 文件中要求 $value 并在第三个 perl 文件中使用它
- windows - 使用 OpenSSL 在 Windows 中为 RabbitMQ 创建密钥、证书和 CA 证书
- python - 如何在 Python 中使用 prototbuf 映射?
- python - 当 python.exe 设置为“以管理员身份运行”时,在 Windows 中运行 python 脚本会给出“无法使用 ... 创建进程”
- python - 如何将 os.popen 命令更改为 python 中的子进程命令
- enterprise-architect - 使用 Enterprise Architect 签入(通过 SVN 版本控制)整个包树
- list - 在 SectionList 中,我如何隐藏空部分的部分标题(数据:[])