I have a Python list only filled with floats:
list_num = [0.41, 0.093, 0.002, 1.59, 0.0079, 0.080, 0.375]
I need to round this list down to get:
list_num_rounded = [0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
Problem: Rounding 1.59 to 1.5 is easy to do. However, my problem is with floats that are less than 1.
Question: Basically, I need to round all floats down so that: if the float is < 1, then the rounded version only contains one non-zero number. Is there a way to do this in Python 2.7?
Attempt: Here is what I have tried:
list_num_rounded = []
for elem in list_num:
if elem > 0.01 and elem < 0.1:
list_num_rounded.append(round(elem,2))
if elem > 0.001 and elem < 0.01:
list_num_rounded.append(round(elem,3))
elif elem > 0.1:
list_num_rounded.append(round(elem,1))
However, this gives:
[0.4, 0.09, 0.002, 1.6, 0.008, 0.08, 0.4]
It is rounding 1.59, 0.79 and 0.375 up, but I need a way to only round down. Is there a way to do this?
The list will not contain negative floats. Only positive floats will be present.
About Python's Built-in round() Function. Python Programming offers a built-in round() function which rounds off a number to the given number of digits and makes rounding of numbers easier. The function round() accepts two numeric arguments, n and n digits and then returns the number n after rounding it to ndigits.
We will use %. 2f to limit a given floating-point number to two decimal places.
You could use logarithms to work out how many leading zeroes there are, then you need a way to round down. One way is to use floor like so:
import math
list_num = [0.41, 0.093, 0.002, 1.59, 0.0079, 0.080, 0.375, 0, 10.1, -0.061]
def myround(n):
if n == 0:
return 0
sgn = -1 if n < 0 else 1
scale = int(-math.floor(math.log10(abs(n))))
if scale <= 0:
scale = 1
factor = 10**scale
return sgn*math.floor(abs(n)*factor)/factor
print [myround(x) for x in list_num]
Output:
[0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
I'm not sure how you want to handle negative numbers and numbers greater than 1, this rounds negative numbers up and numbers greater than 1 to 1dp.
Given that all of the floats are positive you could convert them to strings and use slicing like this.
def round(num):
working = str(num-int(num))
for i, e in enumerate(working[2:]):
if e != '0':
return int(num) + float(working[:i+3])
list_num = [0.41, 0.093, 0.002, 1.59, 0.0079, 0.080, 0.375]
new_list = [round(x) for x in list_num]
print new_list
prints
[0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
If there could be floats in the list with no non-zero values after the decimal you will need to add a simple check to handle that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With