Print Number without Decimal in Python

Python provides a float data type that allows the storage of floating-point numbers. The data at the decimal places is also retained while using the float data type. However, sometimes there is a need to access only the whole number rather than including the decimal part.

This tutorial discusses the different ways available to print number without decimal in Python.

How To Print a Number without Decimal in Python?

There are four different ways available to print a number without decimal in Python. Some of these ways are by utilizing the in-built functions provided by Python, while others may need external functions that can be accessed by importing a simple module in Python.

Using the int() Function

The int() function is utilized to convert the given specified variable into an integer variable. This means that after the conversion process, the obtained value would be an integer and only retain the whole number part while getting rid of the decimal part.

It is important to note that the int() function carries out this process by truncating the value of the given float number toward zero.

The following code uses the int() function to print a number without decimal in Python.

Output

27

The int() function works perfectly well with negative numbers as well. It provides the expected answer that we are looking for as well as removes the decimal part of the given float number.

The following code uses the int() function to print a negative number without decimal in Python.

Output

-27

Using the math.trunc() function

The math.trunc() function from the math module is utilized to remove the fractional part of the given float number and provides only the integer part of the number.

Similar to the int() function, the trunc() function works perfectly and provides the expected answers in the cases of both positive and negative numbers.

It is important to note that to implement this method, we need to import the math module to the Python code first.

The following code uses the math.trunc() function to print a number without decimal in Python.

Output

27
-27

One might argue that the math.floor() function provided by the same math module can function in a similar way but that is not the case. The math.floor() function is able to do only half the work.

In simple terms, the math.floor() function is able to return the largest integer which is less than or equal to the given float number.

This definition restricts the math.floor() function to provide the same results when dealing with negative numbers.

The following code narrates the use of the math.floor() function to print a number without decimal in Python.

Output

27
-28

Due to this, the math.floor() function is considered a bad choice and the math.trunc() function has an upper hand in our case.

Using the round() function

The in-built round() function provided by Python is also capable of providing a concrete result and printing a number without decimal in Python.

The round() function in Python is capable of rounding off a float up and down to the closest integer value.

The following code uses the round() function to print a number without decimal in Python.

Output

27
-27

Although the functionality of the round() function is a bit different from the other methods mentioned in this article, it does fulfill the requirements and provides the expected results.

Using the split() function along with the int() function

A string is also capable of storing a floating-point number and is able to retain the decimal part as well. In cases like that, we can make use of the split() function along with the int() function to implement the task at hand.

Alternatively, we can also convert the float variable into a string variable first and proceed with this method, which makes it quite versatile.

This way comes in handy when there is a need to print both the decimal and non-decimal parts of a given number.

The following code uses the split() function along with the int() function to print a number without decimal in Python.

Output

27
16

Conclusion.

This article provides four different approaches to implementing the task of printing a number without decimal in Python. The first three approaches deal with float data type while the fourth approach mainly tackles the strings that store floating-point values.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *