Print python вывод float

How to print a float variable using the % formatting?

I’m using Python 3.7.1 on Windows. When I print a float variable using the % formatting. only the natural part is printed. Here is an example:

result_2 = 4.523529411764706 statement_2a = "Your text contains an average length of %d letter(s) per words." % result_2 print(result_2) print(statement_2a) 
4.523529411764706 Your text contains an average length of 4 letter(s) per words. 
Your text contains an average length of 4.5235 letter(s) per words. 
  1. How do I prevent the print() function from practically deleting the decimal numbers?
  2. How to round the number to four decimal digits

4 Answers 4

There are several options to evaluate expressions and print them as a string in python.

There are already some good answers, but here are some explicit examples and links to the documentation.

Formatted string literals (f-strings)

f-strings allow you to input expressions which are evaluated at run-time. In the f strings expressions are encased by curly brackets.

x = 42.222222222 print(f'My value is: ') 

prints My value is: 42.222222222 .

and with specifying the format:

x = 42.222222222 print(f'My value is: ') 

Str formatting method

Strings have a built-in .format() method where you can specify replacement fields with curly brackets.

x = 42.222222222 print('My value is: <>'.format(x)) 

prints My value is: 42.222222222 .

and with string formatting:

x = 42.222222222 print('My value is: '.format(x)) 

String formatting operator

x = 42.222222222 print('My value is: %' % x) 

prints My value is: 42.222222222 .

and with string formatting:

x = 42.222222222 print('My value is: %.2f' % x) 

See @Felk answer for some more qualitive descriptions of the different methods.

As you try print float number, use %f instead of %d. This code will print the number to 4 decimal places:

result_2 = 4.523529411764706 statement_2a = "Your text contains an average length of %.4f letter(s) per words." % result_2 print(result_2) print(statement_2a) 

You are using «old-style» formatting and used %d as the placeholder. This will represent your number as a decimal number without any fractions. If you want to display floating point numbers, the placeholder is simply %f .

Читайте также:  Object class and its method in java

If you want to use the variable’s string representation, you can also always just use %s . But since you are on python 3.7, there are some more modern approaches as well.

  • «old-style» formatting: «%s» % var
  • «new-style» formatting using format() : «<>«.format(var) (Read up on python formatters for details)
  • f-strings, which are basically syntactic sugar for format() -based string interpolation. You prefix with f and put the variables in the string literal itself: f»»

A better alternative than % formatting would be to use .format .

result_2 = 4.523529411764706 print(result_2) print("Your text contains an average length of <> letter(s) per words.".format(result_2)) 
result_2 = 4.523529411764706 print(result_2) print("Your text contains an average length of <> letter(s) per words.".format(round(result_2,4))) 

But if you feel comfortable with % formatting then to print whole number use %s

result_2 = 4.523529411764706 statement_2a = "Your text contains an average length of %s letter(s) per words." % result_2 print(result_2) print(statement_2a) 

For rounding to 4 digits use %1.4f

result_2 = 4.523529411764706 statement_2a = "Your text contains an average length of %1.4f letter(s) per words." % result_2 print(result_2) print(statement_2a) 

Источник

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.

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.

# Print the float values: 28.12 print(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.

[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.

# Assign the float value 28.12 to the variable fltNumber fltNumber = 28.12 # Print the float values: 28.12 print(fltNumber)

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.

Читайте также:  Javascript show all objects

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

[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.

# 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))
28 Type of number: 28.0 Type of number after changed:

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

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

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

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.

I am William Nguyen and currently work as a software developer. I am highly interested in programming, especially in Python, C++, Html, Css, and Javascript. I’ve worked on numerous software development projects throughout the years. I am eager to share my knowledge with others that enjoy programming!

Источник

How to Format Float Values in Python

Here are 4 methods to format float in Python.

  1. Using the “format()” function
  2. Using the “%f” format specifier
  3. Using the “round()” function
  4. Using the “f-string” function
Читайте также:  Как включить utf 8 html

Method 1: Using the format() function

To format float values in Python, you can use the “format()” method. The format() method allows multiple substitutions and value formatting.

Example

x = 211911461911461819112146 y = 2**70 z = x / y print("".format(z))

The output returns a string. To get the output as float, use the float() function.

x = 211911461911461819112146 y = 2**70 z = x / y print(float("".format(z)))

Method 2: Using the “%f” format specifier

To format the float value up to two decimal places in Python, you can use the “%.2f” format specifier inside the “print()” function.

Example

x = 211911461911461819112146 y = 2**70 z = x / y print("%.2f" % z)

To format up to three decimal places, use the %.3f.

x = 211911461911461819112146 y = 2**70 z = x / y print("%.3f" % z)

You can see that when we are printing only two decimal places, it automatically rounds to the nearest integer.

Still, printing a number up to three decimal places does not go round to the nearest integer. The 0.496 prints as it is since we want a floating value up to three decimal places.

How to Use the %d Formatter in Python

You can also use floating point numbers in Python with the “%d” formatter. This returns the whole number in a floating point number.

floatNumber = 2.9876 print("%d" % floatNumber) 

Method 3: Using the round() function

The “round()” is a built-in Python method that returns the floating-point number rounded off to the given digits after the decimal point.

Example

x = 211911461911461819112146 y = 2**70 z = x / y print(round(z, 2))

But please note that the behavior of the round() function for floats can be surprising in some cases. For example, round(3.575, 2) gives 3.57 instead of the expected 3.58.

This is not a bug: it’s a result that most decimal fractions can’t be represented exactly as a float.

Method 4: Using the Python f-strings

Python f-String is an improvement over previous formatting methods. You can read more at PEP8.

Let’s use the f-strings to format the float value.

Example

x = 211911461911461819112146 y = 2**70 z = x / y print(f'')

It works well with long calculations with operators and does not need parenthesis.

Conclusion

You can use the %f format specifier or string.format() function to format the float values in Python. You can also use the round() method or the f-string approach.

Источник

Оцените статью