MRが楽しい

MRやVRについて学習したことを書き残す

HoloLens2でホロモンアプリを作る その25(ホロモンの身長を数値に合わせて四捨五入する)

本日はアプリ作成枠です。
HoloLens2でホロモンアプリを作る進捗を書き留めていきます。
f:id:bluebirdofoz:20210404230446j:plain

今回はホロモンの身長を四捨五入するメモです。

ホロモンの身長を数値に合わせて四捨五入する

ホロモンアプリではホロモンが肉を食べると一定の増加率でホロモンが成長します。
そのとき、身長の値に端数が発生するので、数値に合わせて四捨五入する関数を追加しました。
・HoloMonConditionBodySingleton.cs

        /// <summary>
        /// 身長の値をルールに基づいて四捨五入する
        /// </summary>
        /// <returns></returns>
        private float RoundHeight(float a_Height)
        {
            float roundHeight = 0.0f;

            // 1m 未満の場合は小数点第三位を四捨五入する
            if (a_Height < 1.0f)
            {
                roundHeight = (float)System.Math.Round(a_Height, 2, MidpointRounding.AwayFromZero);
            }
            else
            {
                // 1m ~ 10m の場合は小数点第二位を四捨五入する
                if(a_Height < 10.0f)
                {
                    roundHeight = (float)System.Math.Round(a_Height, 1, MidpointRounding.AwayFromZero);
                }
                // 10m 以上は小数点第一位を四捨五入する
                else
                {
                    roundHeight = (float)System.Math.Round(a_Height, MidpointRounding.AwayFromZero);
                }
            }

            return roundHeight;
        }

System.Math の Round 関数で端数の処理を行います。
このとき、MidpointRounding.AwayFromZero のモードを指定することで指定の桁を四捨五入することができます。
docs.microsoft.com

以下の通り、ホロモンがお肉を食べて成長すると、身長の値が四捨五入されています。
f:id:bluebirdofoz:20210404230600j:plain