LoginSignup
1
2

More than 5 years have passed since last update.

UnityのRoundToIntで四捨五入する際の「銀行家の丸め」メモ

Last updated at Posted at 2018-01-12

UnityのRoundToIntで四捨五入する際の「銀行家の丸め」メモ

11.5f は 12 で、 10.5f は 10 となる

実際にDebug.Logで計算結果を出してみるとこんな感じ


Debug.Log(Mathf.RoundToInt(10.0F)); // 10
Debug.Log(Mathf.RoundToInt(10.2F)); // 10
Debug.Log(Mathf.RoundToInt(10.7F)); // 11
Debug.Log(Mathf.RoundToInt(10.5F)); // 10
Debug.Log(Mathf.RoundToInt(11.5F)); // 12
Debug.Log(Mathf.RoundToInt(-10.0F)); // -10
Debug.Log(Mathf.RoundToInt(-10.2F)); // -10
Debug.Log(Mathf.RoundToInt(-10.7F)); // -11
Debug.Log(Mathf.RoundToInt(-10.5F)); // -10
Debug.Log(Mathf.RoundToInt(-11.5F)); // -12

四捨五入では0.5を常に切り上げますが、Roundは丸めた計算結果が偶数になるような丸め方をしているようです。

「最近接偶数への丸め」「最近偶数丸め」「銀行家の丸め」とも言われるようです。

参考にさせて頂いたサイトm( _ _ )m:

1
2
0

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
2