round() 四捨五入
最終更新:2026-09-22
round() 四捨五入
round(number[, ndigits]) は「銀行家の丸め」で指定桁数の小数を返す。ndigits が None のときは整数を返す。
| カテゴリ | 組み込み関数 |
|---|---|
| 種類 | 組み込み関数 |
| Python バージョン | all |
📝 構文
round(number[, ndigits])
⚙️ 引数
| number 必須 | The number to round (int / float). |
|---|---|
| ndigits 任意 | Number of decimal places; None returns an int.(デフォルト値:None) |
戻り値:An int when ndigits is None; otherwise the rounded value of the same type as number (banker's rounding).
💥 例外
TypeError— Raised when ndigits is not an integer.OverflowError— Raised when ndigits is too large (e.g., 10**30).
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
4
4
3.14
ℹ️ 説明 Python 3 は「四捨六入五成双」(銀行家の丸め)を用い、長期的な統計的偏りを避ける。
🏷️ 関連する組み込み関数
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qround(2.5) が 3 ではなく 2 なのはなぜ?
APython 3 は銀行型丸め(五捨五入)を使います。ちょうど .5 のときは最も近い偶数へ丸めるため、2.5 の近い偶数は 2 です。同様に round(3.5) は 4 になります。
Qround(3.14159, 2) は何を返しますか?
A3.14 を返します。2番目の引数 ndigits で保持する小数桁数を指定します。
Qround(123, -1) は何を返しますか?
A120 を返します。ndigits は負にもでき、十の位・百の位などの整数位へ丸めます。
Qround() は正確な十進丸めですか?
A違います。浮動小数点数には二進表現の誤差があるため、round(2.675, 2) が 2.68 ではなく 2.67 になることがあります。金額など精度が重要な場合は decimal.Decimal を使います。