How To Print Fractional Part In Python

In this tutorial, you will learn how to print fractional part in Python.

In Python, there are four ways to print the fractional part of the float number. Python has a lot of built-in functions that you can use to only print the fractional part of a float number. These include modulo (%), round(), math.modf(), and divmod ().

Let us see each method described above with the Code example below.

1. Using divmod() Function

In Python divmod() takes two arguments one is the float value from which you want to extract the decimal value, another argument is the modulo element, in this case, it will be 1. Then this method returns the tuple that contains the decimal value and the fractional value separately.

Let us see in the below example code how divmod function works to print the fractional part in Python.

#Initializing The Float Value
floatValue = 12.3456

#Printing Only Fractional Part
#Using DivMod
onlyFractional = divmod(floatValue, 1)

#Priting the Tuple
print("The Tuple is {}".format(onlyFractional))

#Print Only Fractional Part
print("The Fractional Part is {:0.4f}.".format(onlyFractional[1]))

Output

The Tuple is (12.0, 0.3456)
The Fractional Part is 0.3456.

As you can see in the above code, the tuple was returned that contained both the decimal part and fractional part of the float value. You can either print the decimal part or the fractional part whichever suits your need.

2. Using Modulus (%) To Print Fractional Part

This method is the naive method that is basically a mathematical way to extract the fractional part from the float number. Let us see in the below code how modulus works to get the fractional part of the floating number.

#Initializing The Float Value
floatValue = 12.3456

#Printing Only Fractional Part
#Using DivMod
onlyFractional = floatValue % 1

#Print Only Fractional Part
print("The Fractional Part is {:0.4f}.".format(onlyFractional))

Output:

The Fractional Part is 0.3456.

As you can see in the above code, I was able to extract only the fractional value from the float value using the modulus (%).

3. Using The math.modf Function

This function is part of the Math library in Python. It takes exactly one argument that is the input float value from which you want to extract the decimal value and fractional value. It works similarly to the modulus method discussed above.

This method also returns the tuple that contains the fractional and decimal parts of the input number.

Let us see in the below code example how to use math.modf to print fractional part of a float value in Python.

#importing Math Library
import math

#Initializing The Float Value
floatValue = 12.3456

#Printing Only Fractional Part
#Using DivMod
onlyFractional = math.modf(floatValue)

#Print the Tuple
print("The Tuple is {}".format(onlyFractional))

#Print Only Fractional Part
print("The Fractional Part is {:0.4f}.".format(onlyFractional[0]))

Output:

The Tuple is (0.34559999999999924, 12.0)
The Fractional Part is 0.3456.

As you can see in the above code, only provides the float value to the function math.modf(), It returned the tuple that contained both fractional and decimal values from there you can print the fractional value.

4. Using Round Function To Extract Fractional Part

This can also be done in Python with the round function. Takes one argument and converts the float number to a decimal number with it. As soon as you know how many digits are in a number, you can subtract that number from that number to get its fractional part.

Let us see in the below example code how the round function works to print the fractional part of the number in Python.

#Initializing The Float Value
floatValue = 12.3456

#Printing Only Fractional Part
#Using DivMod
onlyFractional = floatValue - round(floatValue)

#Print Only Fractional Part
print("The Fractional Part is {:0.4f}.".format(onlyFractional))

Output:

The Fractional Part is 0.3456.

As you can see in the above code, I was able to round the input number to its decimal number and then subtracted that decimal number with the input number to get the fractional part of the number.

How To Print Fractional Part In Python

Wrap Up

I hope you now know how to print the fractional part of any number in Python. I’ve listed four methods, with the first three being recommended and the fourth being optional because none of the methods make a significant difference in the code’s efficiency.

Let me know in the comment section if you know any better method than the one discussed above, I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. [Fix] SyntaxError: unexpected EOF while parsing in Python
  2. Convert All Strings in a List to Int in Python
  3. Working With The Current Directory In Python

Leave a Comment