## python：将/ float / string格式化为特定的小数/分数？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (195)

### 2 个回答

```import decimal

def rnd(f,n,r=decimal.ROUND_HALF_DOWN):
pat='1.'+'0'*n
return str(decimal.Decimal(str(f)).quantize(decimal.Decimal(pat),r))```

```for n in (500.99,100.39,5.019):
for i in (0,1,2):
print('n={:10}, i={:2}, Down: {:>10}, Closest: {:>10}'.format(n,i, rnd(n,i,decimal.ROUND_FLOOR),rnd(n,i)))   ```
```n=    500.99, i= 0, Down:        500, Closest:        501
n=    500.99, i= 1, Down:      500.9, Closest:      501.0
n=    500.99, i= 2, Down:     500.99, Closest:     500.99
n=    100.39, i= 0, Down:        100, Closest:        100
n=    100.39, i= 1, Down:      100.3, Closest:      100.4
n=    100.39, i= 2, Down:     100.39, Closest:     100.39
n=     5.019, i= 0, Down:          5, Closest:          5
n=     5.019, i= 1, Down:        5.0, Closest:        5.0
n=     5.019, i= 2, Down:       5.01, Closest:       5.02```

```import math

def rnd(n,f,prec,direction='down'):
if direction=='down':
if prec==0:
return str(int(n))
return str(round(n-math.fmod(n,f),prec))
else:
if prec==0:
return str(int(round(n)))
n+=f
return str(round(n-math.fmod(n,f),prec))```

```>>> rnd(500.99,0.1,0)
'500'
>>> rnd(500.99,0.1,0,'')
'501'
>>> rnd(100.39,0.2,1,'')
'100.4'
>>> rnd(100.39,0.2,1)
'100.2'
>>> rnd(100.59,0.2,1,'')
'100.6'
>>> rnd(100.59,0.2,1)
'100.4'
>>> rnd(5.019,0.01,2)
'5.01'
>>> rnd(5.019,0.01,2,'')
'5.02'```
```def rnd(n,f,prec):
n1=int(n) if prec==0 else round(n-math.fmod(n,f),prec)
n2=float(int(round(n))) if prec==0 else round((n+f)-math.fmod((n+f),f),prec)
return str(min((n1,n2), key=lambda e: math.fabs(e-n)))```

```def round_accordingly(num):

if num>=500:
return str(round(num))
elif num>=100:
return str(round(num, 1))
elif num>=5:
return str(round(num, 2))

print(round_accordingly(550.73424))
print(round_accordingly(200.234245))
print(round_accordingly(7.564743))```