LoginSignup
1
3

More than 3 years have passed since last update.

Pythonでmathモジュールを使わずに四捨五入/切り捨て/切り上げ

Last updated at Posted at 2019-09-20

Pythonでmathモジュールを使わずに四捨五入、切り捨て、切り上げするための方法を記します。

A, B = 12, 100
# 四捨五入
res = (x*2+1)//2
# 切り捨て
res = B//A
# 切り上げ
res = -(-B//A)

追記

round関数は四捨五入ではないというご指摘をいただいたので、修正しました。roundは端数が0.5だと結果が偶数となる方に丸めるみたいです(端数処理)。

1
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3