Home > Backend Development > Python Tutorial > How to output fraction form in python

How to output fraction form in python

藏色散人
Release: 2019-10-19 16:23:25
Original
10255 people have browsed it

How to output fraction form in python

#How to output fraction form in python?

Python fraction representation and writing method

The Fraction function is a module (module) that implements fractions in python. The module is written by others and can Code programs that are used directly, including definitions of classes, functions, and labels, are part of the Python standard function library. The module must be inserted first to use it.

>>> from fractions import Fraction   #插入模块
 
>>> f =Fraction(1,2)  #创建Fraction 类,并初始化为1/2
 
>>> f #输出分数类对象Fraction(1, 2)
Copy after login

Related recommendations: "Python Tutorial"

When entering fractions, the first letter of Fraction must be capitalized, otherwise an error will be reported. We operate on fractions.

>>> Fraction(2,8)+1+1.5
 
2.75
Copy after login

If the float data type appears during the operation, the final result will be float type data. If there are only integer types and fraction types in the operation expression, the output result will be of type fraction.

>>> Fraction(2,8)+Fraction(2,5)+3
 
Fraction(73, 20)
Copy after login

Fraction() method is very interesting, it can directly receive the fraction string and turn it into input. Note that the entered fraction must be enclosed in quotation marks.

>>> a = Fraction('1/5')
 
>>> a
 
Fraction(1, 5)
 
>>>
Copy after login

Input decimals directly, and the Fraction method directly converts decimals into fractions.

>>> from decimal import Decimal
 
>>> Fraction(1.1)
 
Fraction(11, 10)
Copy after login

To convert a string into a decimal, you need to use the decimal class, and then convert the decimal into a fraction.

>>> from decimal import Decimal
 
>>> Fraction(Decimal('1.1'))
 
Fraction(11, 10)
Copy after login

First convert the 1.1 string into a decimal, and then into a fraction.

Extensions:

Python: Fraction operations

fractions module can be used to perform mathematical operations involving fractions

>>> from fractions import Fraction
>>> a = Fraction(5, 4)
>>> b = Fraction(7, 16)
>>> print(a + b)
27/16
>>> print(a * b)
35/64
>>> # Getting numerator/denominator
>>> c = a * b
>>> c.numerator
35
>>> c.denominator
64
>>> # Converting to a float
>>> float(c)
0.546875
Copy after login

Generally not used in most programs There will be problems with calculating fractions, but sometimes they still need to be used. For example, in a program that allows you to accept test units in the form of fractions and perform operations as fractions, using fractions directly can reduce the work of manual conversion to decimals or floating point numbers

The above is the detailed content of How to output fraction form in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template