回答編集履歴
1
追記
test
CHANGED
@@ -25,3 +25,47 @@
|
|
25
25
|
print('{}'.format(v)) # -> 1.888
|
26
26
|
|
27
27
|
```
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
--- 追記
|
32
|
+
|
33
|
+
改めた点はlocalcontext()/round()を使わず、quantize()メソッドにしたこと。それと負の場合に切り捨てにならないので、ROUND_DOWNに変更しています。動作確認はPython3.5.1とPython2.7.11。
|
34
|
+
|
35
|
+
```python
|
36
|
+
|
37
|
+
from decimal import (Decimal, ROUND_DOWN)
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def round_down3(value):
|
42
|
+
|
43
|
+
value = Decimal(value).quantize(Decimal('0.001'), rounding=ROUND_DOWN)
|
44
|
+
|
45
|
+
return str(value)
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
print(round_down3(188888.8888888))
|
50
|
+
|
51
|
+
print(round_down3(1.8888899999))
|
52
|
+
|
53
|
+
print(round_down3(0.9999999999))
|
54
|
+
|
55
|
+
print(round_down3(-1.9999999999))
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
# 出力
|
60
|
+
|
61
|
+
# 188888.888
|
62
|
+
|
63
|
+
# 1.888
|
64
|
+
|
65
|
+
# 0.999
|
66
|
+
|
67
|
+
# -1.999
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
|