如何在Python中使用Decimal进行浮点转换?

noo*_*ter 28 python type-conversion

我需要在Python中将Decimal值转换为float.有没有可用于此类型转换的方法?

Amr*_*gdy 23

有两种方法

1- float_number = float(
decimal_number )2- float_number = decimal_number*1.0

第一个使用内置函数,第二个没有任何功能,但需要关注1.0而不是1,因为它应该是浮动的,因此结果将是浮点数

  • 顺便说一句`float_number = decimal_number*1.0`在python 2 - >不运行的操作数类型中不起作用 (6认同)
  • 隐式转换 (Decimal * 1.0) 在 python 3.5 或 3.6 中不起作用 (4认同)
  • @ PapeK24在python 3中也不起作用! (3认同)

Dan*_*iel 18

只需使用float:

float_number = float(decimal_number)
Run Code Online (Sandbox Code Playgroud)


Ahs*_*que 5

只需将其投射到浮动:

 number_in_float = float(number_you_have)
Run Code Online (Sandbox Code Playgroud)