How To Print Float Values in Python

Python supports not only integers but also floating-point numbers. Many new developers don’t know how to print float values in Python. In this article, we will share with you different ways to get this work done! Scroll down to read!

How to print float values in Python

Below is how to print float values in Python.

Add the floating point number in print() function directly

You use the print() function to print the float values to the screen. This function takes parameters of different data types, including string, integer, list, floating point number, and so on.

The syntax is like this:

print([floating point number])

You add the float values into the print() function directly. Don’t put the float in between the "" or else the values will be in string format.

Here is the code sample:

# Print the float values: 28.12
print(28.12)

The output will be:

28.12

Assign the float value to a variable and put this variable in print() function

You can create a variable and assign float values to it. And then use the variable as the parameter for the print() function. The print() will show the float value on the screen.

The syntax is like this:

[variable] = [floating point number]
print([variable])

Remember to put the variable directly inside the () of the print() function. Don’t place it between "" or else the function will just print out the variable name in string format.

Here is the code sample:

# Assign the float value 28.12 to the variable fltNumber 
fltNumber = 28.12

# Print the float values: 28.12
print(fltNumber)

The output will be:

28.12

Print the float value of an integer number

Python has a built-in function, which is known as float(). This function allows the developers to force the type of any number to the float format. 

For example, you have an integer number, which is 2. The float() function will convert this number to 2.0.

Here is the syntax:

[variable] = [int]
[variable] = float([variable])
print([variable])

You assign the integer value to a variable. And then use the variable as the parameter for the float() function. It will turn the number into a floating-point number format. This time, if you print the variable, the output will show a float value.

Here is the code sample:

# Assign the int value: 28 to the variable: number
number = 28
print(number)
print("Type of number: ", type(number))

# Convert the int to float values
number = float(number)

# Print the float values: 28.0
print(number)
print("Type of number after changed: ", type(number))

The output will be:

28
Type of number:  <class 'int'>
28.0
Type of number after changed:  <class 'float'>

Of course, you can convert an int to a float directly, using the following syntax:

print(float([integer number]))

The int number will be converted to float before it is printed out to the screen.

Here is the code sample:

# Convert the int: 28 to float value and print it out
print(float(28))

The output will be:

28.0

Summary

To summarize, we have shown how to print float values in Python. You can print the float directly or print a variable that has the float value assigned. On the other hand, you can take advantage of the float() function to convert int value to float value.

Leave a Reply

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