アストラルプリズム

PC、スマホ、ゲームなどの備忘録と日記

python 四捨五入と切り上げ、切り捨て

四捨五入

import math
math.round(4.5)
結果 4
math.round(4.6)
結果 5
pythonの四捨五入は五捨六入なので要注意
桁の指定は
math.round(4.563,2)
結果 4.56

切りあげ

math.ceil(2.2)
結果 3

切り捨て

math.floor(2.2)
結果 2

mathを付けたらながくて困る場合
from math import sin,cos,tan,acos,pi,ceil
なぜかblender2.81ではroundはfromなにがししなくても
round()だけで動くの不思議

値を指定した範囲にしたい場合
a=190
min( max(a,0), 180 )
結果 180
三角関数で値の範囲が決まってるけど誤差で180こえちゃうときとかに便利