python
# 指定した桁を0か5に丸める。
# 123で1の位を丸める場合は、digits=1。10の位をを丸める場合はdigits=2
def roundIntZeroOrFive(x: int, digits: int) -> int:
n = int(f"{x:0{digits}}"[-digits])
d = int(x / 10**(digits))
# if 0 <= n <= 2:
# d += 0
if 3 <= n <= 7:
d += 0.5
elif 8 <= n <= 9:
d += 1
return int(d * 10**digits)
テストコード
def test(digit, times=10, minN=1, maxN=1000):
for i in range(times):
n = random.randrange(minN, maxN)
print(f"{n:4} -> {roundIntZeroOrFive(n, digit):4}")
print("")
test(1)
test(2)
test(3)
test(4)
468 -> 470
409 -> 410
808 -> 810
154 -> 155
504 -> 505
519 -> 520
241 -> 240
342 -> 340
846 -> 845
634 -> 635
709 -> 700
443 -> 450
521 -> 500
634 -> 650
230 -> 250
258 -> 250
64 -> 50
852 -> 850
612 -> 600
446 -> 450
447 -> 500
713 -> 500
821 -> 1000
437 -> 500
744 -> 500
942 -> 1000
613 -> 500
338 -> 500
716 -> 500
513 -> 500
979 -> 0
969 -> 0
785 -> 0
542 -> 0
235 -> 0
263 -> 0
674 -> 0
40 -> 0
456 -> 0
401 -> 0