ºÇ½ª¹¹¿·¡§
orika_ex_miyako 2021ǯ05·î24Æü(·î) 20:20:44ÍúÎò
¤½¤Î¤¿¤á¼«ºî¤µ¤ì¤Æ¤¤¤ëÊý¤â¿¤¤¤È»×¤¤¤Þ¤¹¤¬¡¢»Í¼Î¸ÞÆþ·×»»ÍѤΥ桼¥Æ¥£¥ê¥Æ¥£¥¯¥é¥¹¤È·×»»¥á¥½¥Ã¥É¤ÎºîÀ®Îã¤òÄ󼨤¤¤¿¤·¤Þ¤¹¡£
»Í¼Î¸ÞÆþ¤ò¤·¤¿¤¤¾®¿ôÅÀ¤Î·å¿ô¤ò»ØÄꤷ¤Æ·×»»¤Ç¤¤ëÅÀ¤¬¥Ý¥¤¥ó¥È¤Ë¤Ê¤ê¤Þ¤¹¡£
public class CalcUtility { /// <summary> /// »ØÄꤷ¤¿¾®¿ôÅÀ¤Î·å¿ô¤Ë¤Ê¤ë¤è¤¦¤Ë¡¢£±¤Ä²¼¤ÎÃͤò»Í¼Î¸ÞÆþ¤·¤¿ÃͤòÌ᤹ /// </summary> /// <param name="value">·×»»¤¹¤ëÃÍ</param> /// <param name="digitsNum">»Ä¤·¤¿¤¤¾®¿ôÅÀ¤Î·å¿ô(¤³¤Î£±¤Ä²¼¤ÎÃͤò»Í¼Î¸ÞÆþ¤¹¤ë)</param> /// <returns></returns> public static float CalculateRoundHalfUp(float value, int digitsNum) { return float.Parse(value.ToString("F" + digitsNum.ToString())); } }
¡¡Â裱°ú¿ô¤Ë¤Ï·×»»¤·¤¿¤¤ float ·¿¤ÎÃͤò»ØÄꤷ¤Þ¤¹¡£Â裲°ú¿ô¤Ë¤Ï»Í¼Î¸ÞÆþ¤·¤Æ»Ä¤·¤¿¤¤¾®¿ôÅÀ¤Î·å¿ô¤ò»ØÄꤷ¤Þ¤¹¡£(»Í¼Î¸ÞÆþ¤¹¤ë·å¿ô¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó)
Î㤨¤Ð¡¢digitsNum ¤ÎÃͤ¬ 2 ¤Ç¤¢¤ë¾ì¹ç¤Ë¤Ï¾®¿ôÅÀÂè3°Ì¤ò»Í¼Î¸ÞÆþ¤·¤Æ¡¢Âè2°Ì¤Þ¤Ç¤ò»ý¤ÄÃͤËÊÑ´¹¤¹¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
¡¡´Êñ¤Ê»ÈÍÑÎã¤Ç¤¹¡£
¡ã¤½¤Î£±¡ä
¡¡float x = 0.5522f; int digitsNum = 3; x = CalcUtility.CalculateRoundHalfUp(x, digitsNum); Debug.Log(x);
¡ãConsole ¥Ó¥å¡¼¡ä
0.552
¡ã¤½¤Î£²¡ä
¡¡float x = 0.42f; float y = 0.45f int digitsNum = 1; if(CalcUtility.CalculateRoundHalfUp(x, digitsNum) >= 0.5f){ Debug.Log("x"); } if(CalcUtility.CalculateRoundHalfUp(y, digitsNum) >= 0.5f){ Debug.Log("y"); }
¡ãConsole ¥Ó¥å¡¼¡ä
y
¡¡¾®¿ôÅÀ¤ÎÂè2°Ì¤ò»Í¼Î¸ÞÆþ¤·¤ÆÂè1°Ì¤ÎÃͤˤ·¤Þ¤¹¡£¤½¤Î¤¿¤á¡¢x = 0.4f¡¢ y = 0.5f ¤È¤Ê¤ê¤Þ¤¹¤Î¤Ç¡¢
Console ¥Ó¥å¡¼¤Ë¤Ï y ¤¬É½¼¨¤µ¤ì¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯