[TIL] 118 [C#] Math.Round() Bankerโ€™s rounding??, int/int

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,


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;
}





์žก๋‹ด, ์ผ๊ธฐ?

์ด๋ ฅ์„œ ์ •๋ฆฌ, ์ž์†Œ์„œ ์ž‘์„ฑ, ๋ฉด์ ‘์ค€๋น„์ค‘, ์ฝ”ํ…Œ




๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ