[英]Python - How to round down a float to 1 significant digit
I want to round down a float to a specific number of significant digits. 我想将浮点数向下舍入到特定数量的有效数字。 Very similar to this answer: https://stackoverflow.com/a/3411435/281021 but instead of the normal
round()
behavior, it should round down always. 非常类似于这个答案: https : //stackoverflow.com/a/3411435/281021但不是正常的
round()
行为,它应该始终向下舍入。 I can't use math.floor()
because it turns the float into an int. 我不能使用
math.floor()
因为它将float转换为int。
Basically, 0.45
should become 0.4
instead of 0.5
. 基本上,
0.45
应该变为0.4
而不是0.5
。 And 1945.01
should become 1000.0
instead of 2000.0
. 并且
1945.01
应该变为1000.0
而不是2000.0
。
Scientific representation seems to be the way to go, but numerical techniques usually end up faster for me than string techniques. 科学表征似乎是要走的路,但数字技术通常比字符串技术更快。 You do get the occasional floating point error however...
你确实得到偶然的浮点错误...
from math import *
def roundDown(x, sigfigs=1): #towards -inf
exponent = floor(log10(copysign(x,1))) #we don't want to accidentally try and get an imaginary log (it won't work anyway)
mantissa = x/10**exponent #get full precision mantissa
# change floor here to ceil or round to round up or to zero
mantissa = floor(mantissa * 10**(sigfigs-1)) / 10**(sigfigs-1) #round mantissa to sigfigs
return mantissa * 10**exponent
Rounding towards zero or +inf is as easy as changing the floor
to ceil
or round
. 向零或+ inf舍入就像将
floor
改为ceil
或round
一样简单。 Another benefit of computing the mantissa and exponent numerically rather than casting to a string is that the number of sigfigs can be easily changed 以数字方式计算尾数和指数而不是转换为字符串的另一个好处是可以轻松更改sigfigs的数量
Use scientific notion to take the significant digit and the power, and calculate the result 使用科学概念来获取有效数字和功率,并计算结果
def significant_1 (s):
l = len(str(s)) ####make sure there is enough precision
a = ('%.' + str(l) + 'E') % decimal.Decimal(s)
#print (a)
significate_d = a.split(".")[0]
times = a.split("E")[1]
result = int(significate_d) * (10 ** int(times))
return result
print (significant_1(1999))
print (significant_1(1945.01))
print (significant_1(0.45))
output: 输出:
1000
1000
0.4
I think it's a good and simple way: 我认为这是一个很好而简单的方法:
def convert(number, interval):
return int(number/interval)*interval
Sample outputs: 样本输出:
1923,1000 -> 1000
12.45,0.1 -> 12.4
round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after the decimal point. 返回舍入到小数点后的ndigits数字的浮点值数字。 If ndigits is omitted, it defaults to zero.
如果省略ndigits,则默认为零。 The result is a floating point number.
结果是一个浮点数。 Values are rounded to the closest multiple of 10 to the power minus ndigits;
将值四舍五入为函数减去ndigits的最接近的10的倍数; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5) is -1.0).
如果两个倍数相等,则舍入远离0(因此,例如,round(0.5)为1.0,round(-0.5)为-1.0)。
https://docs.python.org/2/library/functions.html#round https://docs.python.org/2/library/functions.html#round
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.