Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

round down to 2 decimal in python

Tags:

I need to round down and it should be two decimal places.
Tried the following,

a = 28.266 print round(a, 2) 

28.27

But the expected value is 28.26 only.

like image 611
Suresh Kumar Avatar asked Dec 29 '16 16:12

Suresh Kumar


People also ask

How do you round to 2 decimal places in Python?

Python's round() function requires two arguments. First is the number to be rounded. Second argument decides the number of decimal places to which it is rounded. To round the number to 2 decimals, give second argument as 2.

How do you round down decimals in Python?

Python has a built-in round() function that takes two numeric arguments, n and ndigits , and returns the number n rounded to ndigits . The ndigits argument defaults to zero, so leaving it out results in a number rounded to an integer.

How do you round down 0.5 in Python?

To round a number down to the nearest 0.5:floor() method passing it the number multiplied by 2 . Divide the result by 2 . The result of the calculation is the number rounded down to the nearest 0.5 .

How to round to 2 decimal places in Python 3?

How to Round to 2 decimal Places python 3 In this example, we are using Python’s inbuilt round () function to round off a float number to 2 decimal places and using the print () function to print the result. 2. How to Round to different decimal places

How do you round up and down in Python?

To round up and down we use Python’s round () function. The first argument we give that function is the number to round. The second argument the number of decimal places to round to.

How to round to a certain number of decimal places?

Though rounding to a whole number is helpful, there are still plenty of situations in which we need to round to a certain number of decimal places. For example, currency is best reported with 2 decimal digits. And error levels and significance often use more digits, like 5. The round () function rounds decimal places up and down.

How to round a number by trunc in Python?

It is a straightforward method that can be used to round a number by truncating a given number of digits. x -> The decimal number that needs to be truncated. In this example, we have used inbuilt math.trunc () method of Python from the math module to obtain the integer part of the given decimal number.


1 Answers

Seems like you need the floor:

import math math.floor(a * 100)/100.0  # 28.26 
like image 167
Psidom Avatar answered Oct 16 '22 05:10

Psidom