[TIL] 118 [C#] Math.Round() Bankerโs rounding??, int/int
์นดํ ๊ณ ๋ฆฌ: Til
MidpointRounding.AwayFromZero
Math.Round()
Math.Round()
๊ฐ์ฅ ๊ฐ๊น์ด ์ง์๋ก ๋ฐ์ฌ๋ฆผ๋๋ค.
-> ๋ฐ์ฌ๋ฆผ ์ง์์0.5๋ ๋ฒ๋ฆฌ๊ณ ํ์๋ ์ฌ๋ฆฐ๋ค.
12.5 => 12
13.5 => 14 ๊ฐ ๋๋ค.
MidpointRounding.AwayFromZero๋ฅผ ์ฌ์ฉํ์ฌ 0.5๋ฅผ ์ฌ๋ฆผ์ผ๋ก ์ฌ์ฉ.
// ์ง์๋ก ๋ฐ์ฌ๋ฆผ
Math.Round(diffs.Average());
// 0.5 ์ฌ๋ฆผ
Math.Round(diffs.Average(), MidpointRounding.AwayFromZero));
int/int
int/int๋ฅผ ํ๋ฉด ๊ฐ์ด ์ ์๋ก ๋์ ์์๋ถ๋ถ์ด ๋ฒ๋ ค์ง๋๋ค.
(float)int/int ๋ฅผ ํตํด ์์๋ถ๋ถ์ ๊ตฌํฉ๋๋ค.
static int average(List<int> list)
{
float avg = ((float)list.Sum() / list.Count());
int a = (int)Math.Round(avg);
return a;
}
์ก๋ด, ์ผ๊ธฐ?
์ด๋ ฅ์ ์ ๋ฆฌ, ์์์ ์์ฑ, ๋ฉด์ ์ค๋น์ค, ์ฝํ
๋๊ธ๋จ๊ธฐ๊ธฐ