Round 在 Python 中向下浮动以仅保留一位非零小数
Round floats down in Python to keep one non-zero decimal only
我有一个 Python 列表,其中只填充了浮点数:
list_num = [0.41, 0.093, 0.002, 1.59, 0.0079, 0.080, 0.375]
我需要将此列表四舍五入得到:
list_num_rounded = [0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
问题:
将 1.59 四舍五入到 1.5 很容易做到。但是,我的问题是浮点数小于 1。
问题:
基本上,我需要将所有浮点数向下舍入,以便:如果浮点数 < 1,则四舍五入后的版本仅包含一个非零数字。有没有办法在 Python 2.7 中做到这一点?
尝试:
这是我尝试过的:
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))
但是,这给出了:
[0.4, 0.09, 0.002, 1.6, 0.008, 0.08, 0.4]
它向上舍入 1.59、0.79 和 0.375,但我需要一种只向下舍入的方法。有办法吗?
该列表将不包含负浮点数。只会出现正浮点数。
将浮点数格式化为科学记数法会有所帮助;然后转换回浮点类型应该实现你想要的。尝试类似的东西:
eval("%.0e" % (.03))
eval("%.0e" % (.034))
eval("%.0e" % (.0034))
您可以使用对数计算出有多少个前导零,然后您需要一种向下舍入的方法。一种方法是像这样使用 floor:
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]
输出:
[0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
我不确定您要如何处理负数和大于 1 的数字,这会将负数和大于 1 的数字四舍五入为 1dp。
假设所有浮点数都是正值,您可以将它们转换为字符串并像这样使用切片。
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
打印
[0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
如果列表中可能存在小数点后没有非零值的浮点数,您将需要添加一个简单的检查来处理它。
def round_float(num):
if not num:
return num
current_num = abs(num) * 10
round_value = 1
while not (current_num//1):
current_num *= 10
round_value +=1
return round(num, round_value)
我有一个 Python 列表,其中只填充了浮点数:
list_num = [0.41, 0.093, 0.002, 1.59, 0.0079, 0.080, 0.375]
我需要将此列表四舍五入得到:
list_num_rounded = [0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
问题: 将 1.59 四舍五入到 1.5 很容易做到。但是,我的问题是浮点数小于 1。
问题: 基本上,我需要将所有浮点数向下舍入,以便:如果浮点数 < 1,则四舍五入后的版本仅包含一个非零数字。有没有办法在 Python 2.7 中做到这一点?
尝试: 这是我尝试过的:
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))
但是,这给出了:
[0.4, 0.09, 0.002, 1.6, 0.008, 0.08, 0.4]
它向上舍入 1.59、0.79 和 0.375,但我需要一种只向下舍入的方法。有办法吗?
该列表将不包含负浮点数。只会出现正浮点数。
将浮点数格式化为科学记数法会有所帮助;然后转换回浮点类型应该实现你想要的。尝试类似的东西:
eval("%.0e" % (.03))
eval("%.0e" % (.034))
eval("%.0e" % (.0034))
您可以使用对数计算出有多少个前导零,然后您需要一种向下舍入的方法。一种方法是像这样使用 floor:
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]
输出:
[0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
我不确定您要如何处理负数和大于 1 的数字,这会将负数和大于 1 的数字四舍五入为 1dp。
假设所有浮点数都是正值,您可以将它们转换为字符串并像这样使用切片。
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
打印
[0.4, 0.09, 0.002, 1.5, 0.007, 0.08, 0.3]
如果列表中可能存在小数点后没有非零值的浮点数,您将需要添加一个简单的检查来处理它。
def round_float(num):
if not num:
return num
current_num = abs(num) * 10
round_value = 1
while not (current_num//1):
current_num *= 10
round_value +=1
return round(num, round_value)