Python 将分数转换为小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4841732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:47:19  来源:igfitidea点击:

Python Convert fraction to decimal

python

提问by Kartik

I want to convert 1/2 in python so that when i say print x (where x = 1/2) it returns 0.5

我想在 python 中转换 1/2,这样当我说 print x (where x = 1/2) 它返回 0.5

I am looking for the most basic way of doing this, without using any split functions, loops or maps

我正在寻找这样做的最基本的方法,而不使用任何拆分函数、循环或映射

I have tried float(1/2) but I get 0... can someone explain me why and how to fix it?

我试过 float(1/2) 但我得到 0 ... 有人可以解释我为什么以及如何解决它吗?

Is it possible to do this without modifying the variable x= 1/2 ??

是否可以在不修改变量 x= 1/2 的情况下做到这一点??

Thanks

谢谢

采纳答案by Ant

in python 3.x any division returns a float;

在 python 3.x 中,任何除法都返回一个浮点数;

>>> 1/2
0.5

to achieve that in python 2.x, you have to force float conversion:

要在 python 2.x 中实现这一点,您必须强制进行浮点转换:

>>> 1.0/2
0.5

or to import the division from the "future"

或从“未来”导入分区

>>> from __future__ import division
>>> 1/2
0.5

An extra: there is no a built-in fraction type, but there is in the official library:

额外的:没有内置的分数类型,但在官方库中有:

>>> from fractions import Fraction
>>> a = Fraction(1, 2) #or Fraction('1/2')
>>> a
Fraction(1, 2)
>>> print a
1/2
>>> float(a)
0.5

and so on...

等等...

回答by Greg Hewgill

You're probably using Python 2. You can "fix" division by using:

您可能正在使用 Python 2。您可以使用以下方法“修复”除法:

from __future__ import division

at the start of your script (before any other imports). By default in Python 2, the /operator performs integer division when using integer operands, which discards fractional parts of the result.

在脚本的开头(在任何其他导入之前)。默认情况下,在 Python 2 中,/运算符在使用整数操作数时执行整数除法,这会丢弃结果的小数部分。

This has been changed in Python 3 so that /is always floating point division. The new //operator performs integer division.

这在 Python 3 中已更改,因此/始终是浮点除法。new//运算符执行整数除法。

回答by CaptainChristo

Alternatively, you can force floating point division by specifying a decimal or by multiplying by 1.0. For instance (from inside the python interpreter):

或者,您可以通过指定小数或乘以 1.0 来强制进行浮点除法。例如(从python解释器内部):

>>> print 1/2
0
>>> print 1./2
0.5
>>> x = 1/2
>>> print x
0
>>> x = 1./2
>>> print x
0.5
>>> x = 1.0 * 1/2
>>> print x
0.5

EDIT: Looks like I was beaten to the punch in the time it took to type up my response :)

编辑:看起来我在输入我的回复时受到了打击:)

回答by Karl Knechtel

There is no quantity 1/2anywhere. Python does not represent rational numbers with a built-in type - just integers and floating-point numbers. 1 is divided by 2 - following the integer division rules - resulting in 0. float(0)is 0.

1/2任何地方都没有数量。Python 不使用内置类型表示有理数——只是整数和浮点数。1 除以 2 - 遵循整数除法规则 - 结果为 0。float(0)就是 0。

回答by Brian Minton

If the input is a string,then you could use Fraction directly on the input:

如果输入是字符串,则可以直接在输入上使用 Fraction :

from fractions import Fraction

x='1/2'
x=Fraction(x)  #change the type of x from string to Fraction
x=float(x)     #change the type of x from Fraction to float
print x