int(round(x))
丸め、整数に変更します
編集:
int(round(h))を変数に割り当てていません。int(round(h))を呼び出すと、整数が返されますが、それ以外は何も行われません。次の行を変更する必要があります。
h = int(round(h))
新しい値をhに割り当てるには
編集2:
@plowmanがコメントで述べたように、Python round()は通常のように機能しません。これは、数値が変数として格納される方法が通常、画面に表示される方法と異なるためです。この動作を説明する多くの答えがあります:
round()が適切に丸められていないようです
この問題を回避する1つの方法は、この回答で述べられているように10進数を使用することです:https : //stackoverflow.com/a/15398691/4345659
追加のライブラリを使用せずにこの回答を適切に機能させるには、カスタム丸め関数を使用すると便利です。多くの修正の後、私がテストした限り、すべての保存の問題を回避した次の解決策を思いつきました。repr()(NOT str()!)で取得した文字列表現の使用に基づいています。見た目はハックですが、すべてのケースを解決するために私が見つけた唯一の方法でした。Python2とPython3の両方で動作します。
def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
return float(num[:-1])
テスト:
>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>>
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0
最後に、正しい答えは次のとおりです。
# Having proper_round defined as previously stated
h = int(proper_round(h))
編集3:
テスト:
>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1 # should be 7
ここでの落とし穴は、dec-thの10進数が9になる可能性があることです。- dec+1thの桁> = 5の場合、9は0になり、1がdec-1-thの桁に送られます。
これを考慮すると、次のようになります。
def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
a = num[:-2-(not dec)] # integer part
b = int(num[-2-(not dec)])+1 # decimal part
return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
return float(num[:-1])
上述した状況においてb = 10、および以前のバージョンのと同じCONCATENATE aとbの連結をもたらすであろう10後続0が消えてしまう場合。このバージョンは、適切な桁上げとして、にb基づいて正しい小数点以下の桁に変換されdecます。
int(x)