- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display a float with two decimal places in Python?
Numbers with a decimal point are considered floating numbers, also referred to as real numbers. A floating number has six decimal points by default, but we can adjust this using the methods described below.
In this article, we are going to find out how to display a float with two decimal places in Python.
The first technique is to employ the format method. The format will be included in a print statement, and it will be referenced using curly braces, with the number of decimal points mentioned inside the curly braces.
Example
In the example given below, we are taking a floating number as input and we are displaying it with 2 decimal points.
num = 12.263241 print("Given floating number is") print(num) print("The floating number rounded up to 2 decimal points is") print("{:12.2f}".format(num))
Output
The output of the above example is as shown below−
Given floating number is 12.263241 The floating number rounded up to 2 decimal points is 12.26
Using % operator
The second method is to employ the percent operator. It's identical to the format technique, except instead of format, we'll use percent, and instead of curly braces, we'll use percent.
Example
In the example given below, we are taking a floating number as input and we are displaying it with 2 decimal points using % operator −
num = 12.26549 print("Given floating number is") print(num) print("The floating number rounded up to 2 decimal points is") print("% .2f" %num)
Output
The output of the above example is given below −
Given floating number is 12.26549 The floating number rounded up to 2 decimal points is 12.27
Using round() operator
The round operator is used in the third approach. The round operator will take the number and the amount of decimal points as input and return the revised value as output.
Example
In the example given below, we are taking a floating number as input and we are displaying it in only 2 decimal points using round() operator. −
num = 12.26549 print("Given floating number is") print(num) print("The floating number rounded up to 2 decimal points is") print(round(num,2))
Output
The output of the above example is given below −
Given floating number is 12.26549 The floating number rounded up to 2 decimal points is 12.27
- Related Articles
- How to parse float with two decimal places in JavaScript?
- How to display Y-axis labels with more decimal places in R?
- Display 3 decimal places in MySQL?
- Trying to round a calculation to two decimal places in a new column with MySQL?
- How to find the percentage for frequencies stored in a vector with two decimal places in R?
- The decimal expansion of the rational number will $frac{14587}{1250}$ terminate after:(A) one decimal place(B) two decimal places(C) three decimal places(D) four decimal places
- Format amount values for thousands number with two decimal places in MySQL?
- Python program to convert float decimal to octal number
- Display the warning message when a FLOAT value is inserted into DECIMAL in MySQL?
- How to limit Decimal Places in Android EditText?
- How to round a number to n decimal places in Java
- How to format number to 2 decimal places in MySQL?
- How to display numbers with decimal in R data frame column?
- Find the value of $sqrt{5}$ correct to two decimal places, then find the value of $frac{3-sqrt{5}}{3+sqrt{5}}$ correct to two decimal places.
- How to round MySQL column with float values and display the result in a new column?
