[C#] float int 변환

C#/Winform 2022. 2. 17. 13:51
728x90
728x170

float 에서 int 로 변환할때 기본적으로 소수점 뒷자리를 버림을 한다.

float f = 1.547f;
int i = (int)Math.Round(f);
(출력값)
1

하지만 반올림을 하고 싶을 경우 아래처럼 처리하면된다.

float f = 1.547f;
int i = (int)Math.Round(f,MidpointRounding.AwayFromZero);
(출력값)
2

아래처럼 해도 반올림이 된다.

float f = 1.547f;
int i = Convert.ToInt32(Math.Round(f));
(출력값)
2

728x90
그리드형
Posted by kjun
,