[英]Converting last n digits of a number to Zero
What is the best way to replace the last digits of a number to zero keeping first three digits as it is, in python? 在python中,将数字的最后一位数字替换为零以保持前三位数字的最佳方法是什么?
Example: 例:
23456789 -> 23400000
112022 -> 112000
1111-> 1110
111 -> 111 (no conversion)
convert it to a string: 将其转换为字符串:
a = 23456789
a_str = str(a)
sol = a_str[0:3]+"0"*(len(a_str)-3)
print(sol)
returns: 23400000 返回:23400000
round
accepts a negative argument, which is quite useful, but it rounds the result, which may not be exactly what you want: round
接受一个负面的参数,这是非常有用的,但它会对结果进行round
入,这可能不是你想要的:
a = 23456789
round(a, -4)
23460000
If you do not wish to round , you could use a small helper function, maybe like this one: 如果你不想圆 ,你可以使用一个小辅助函数,也许像这样:
import math
def significant(num, signum):
expo = 10**(int(math.log(num, 10)) - signum + 1)
return expo * (num // expo)
significant(12345, 3)
12300
Use round
, but you have to be a bit more clever to get the functionality you want 使用round
,但你必须更聪明才能获得你想要的功能
def round_n(num, keep=3):
return round(num, keep - len(str(num)))
round_n(23456789)
Out[72]: 23500000
round_n(112022)
Out[73]: 112000
round_n(1111)
Out[74]: 1110
round_n(111)
Out[75]: 111
nb this will only work with integers, not floats 这只适用于整数,而不是浮点数
If you just want to truncate, not round, then instead do 如果你只是想截断,而不是圆,那么就做
def truncate_int(n, keep=3):
if n < 0: # account for '-'
keep += 1
s = str(n)
return int(s[:keep] + '0'*(len(s) - keep))
You can define a converter function using math.log10
, floor division and exponentials: 您可以使用math.log10
,floor division和exponentials定义转换器函数:
a = 23456789
b = 112022
c = 1111
d = 111
import math
def converter(x, k=3):
dig = int(math.log10(x)) + 1
n = dig - k
return x // 10**n * 10**n
for val in [a, b, c, d]:
print(val, '->', converter(val))
23456789 -> 23400000
112022 -> 112000
1111 -> 1110
111 -> 111
I would do this using floor division and multiplication with an appropriate multiplier. 我会使用地板除法和乘法与适当的乘数来做到这一点。 That way, we avoid problems with rounding. 这样,我们避免了舍入问题。 We also have to ensure that the multiplier is an integer. 我们还必须确保乘数是一个整数。 We can do that by using max
to force m
to be non-negative. 我们可以通过使用max
来强制m
为非负数。
data = (23456789, 112022, 1111, 111, 6, 98, 987, 9876, 9876598765)
def zero_final(n, digits=3):
m = 10 ** max(0, len(str(n)) - digits)
return n // m * m
for n in data:
print(n, zero_final(n))
output 产量
23456789 23400000
112022 112000
1111 1110
111 111
6 6
98 98
987 987
9876 9870
9876598765 9870000000
Not sure it's the best way, 不确定这是最好的方式,
but I would use string manipulation and formatting. 但我会使用字符串操作和格式化。
number = 23456789
print int(str(number)[:3] + "%0{0}d".format(len(str(number)) - 3) % 0)
You can try using string operation as follows: 您可以尝试使用字符串操作,如下所示:
def number_cov(num):
x = str(num)
if len(x)>3:
cov_num = int(x[0:3]+'0'*(len(x)-3))
else:
cov_num = num
return cov_num
number_cov(11111)
11100
n - n%1000
For a positive integer n
, n%1000
gives you the value of the last three digits. 对于正整数n
, n%1000
给出最后三位数的值。 So subtracting that will give you a number ending in 000
. 所以减去那将给你一个以000
结尾的数字。
>>> n = 12345
>>> n - n%1000
12000
If you want to do this for other numbers of digits, you can do it like this: 如果你想为其他数字做这个,你可以这样做:
>>> n = 1234567
>>> r = 10**5 # I want to zero-out five digits
>>> n - n%r
1200000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.