Get only two digits after the decimal point in Python

Hi Friends! In this tutorial, we are going to learn how to get only two digits after the decimal point in the input.  In many tasks that we solve we may have many mathematical operations involved.  And many operations may require only specific types of inputs.  So, first, let us learn how to take a number as input for any mathematical operation in Python.

Taking a number as input in Python

In order to have a proper understanding of this topic, the reader should be familiar with the data types we have in Python.

If we want to take a number as input in Python then we should take the value into a variable of type integer or float. Since we are discussing number with decimal points. So, we should take float data type into account.

The input can be given either statically or dynamically to a program. Let us see both ways.

Types of inputs in python

Static Input

Static Input: As the name refers statically that is it is given fixed however we can again change the value as the program goes on but at the particular point its value is fixed.  Any changes to the given value can be only done by changing the code. Once the execution begins there is no way for changing the value of the variable at the point of execution in the program. In python for giving the value to a variable in a static way.

We need not specify the data type that it takes as value. This is because python is dynamically written. That is the Python compiler itself checks the data type of the variable at the time of execution. The syntax to give input to a variable will be

x=5.678

Here, not only we are giving the variable x value but also we are initializing it.

Dynamic Input

Dynamic Input: This is quite opposite to static that is the value is not given as fixed in the code.  We can give whatever value at the time of its execution.  The value is given by the user from the keyboard if a dynamic input is given at a particular part of the code in a program.  Then, while executing that part the execution waits until the user gives the input from the keyboard. The syntax to initialize a variable and to take the input from the user will be:

x=float(input("enter a number"))

Since we have to give the value with the decimal point we used float keyword which is used to specify the data type in an abstract way.

So far in this session, we have learned how to give a number as input in python. Now let us learn how we can take only two digits after the decimal point.

To get only two digits after decimal point

In order to get a complete understanding from this session make sure that you are good with operators in Python.  Don’t worry if you are not.  As this tutorial is for beginners.  I will start this section with the description of operators that will be used as a part of our solution to the problem.

The % operator and // operator

The modulus operator represented as ‘/’ is a mathematical operator that gives the result when a number is divided by some number.  And the integer division operator represented by ‘//’ gives the quotient.

For example if

x=7;
y=4;
x//y=1;
x/y=1.75;
This is because when 7 is divided by 4 the quotient is 1 and the result  is a floating point 1.75

Using these operators

Now let us understand some logic required for the operation we require. Here is the logic to be implemented

x=x*100
x=x//1
x=x/100

Here, we first brought the two digits after the decimal point to before the point by multiplying the number with 100. Then, we removed the decimal part from the number by performing integer division with 1. Now, we send back the two digits to right of the decimal point by performing normal division with 100.

In this way, we can only get two digits after the decimal point in input. Now, let’s see what if x=3.5678 then x changes to 356.78 in the first step.  And then it changes to 356 in the next step and then it changes to 3.56 at the last.  Which can be used as the required input.  This can be used for both Static and Dynamic inputs.

Also, read:

 

Hope you like the session we have not only seen the logic for how to take only two digits after decimal point in input. But also some valuable information on data types and also type of user input and operators.

 

Leave a Reply

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