상세 컨텐츠

본문 제목

특정 소수점 자리 이하 올림, 버림, 반올림

C#

by 탑~! 2019. 11. 20. 14:21

본문

System.Mafh. 이후 함수를 사용합니다.

 

 

double doubleValue = 0.1234d;

 

Math.Ceiling(doubleValue) // 올림

 

Math.Round(doubleValue) // 반올림

 

Math.Truncate(doubleValue) // 버림

 

 

만약에 소수점 첫째 자리 이하를 버리고 싶다면

 

Math.Truncate(doubleValue * 10) / 10;

 

소수점 둘째 자리 이하

 

Math.Truncate(doubleValue * 100) / 100;

 

소수점 셋째 자리 이하

 

Math.Truncate(doubleValue * 1000) / 1000;

 

.

.

.

식으로 늘려주면 된다.

 

[ 원리 ]

 

원하는 값 : 0.1234 >>>> 0.12

 

1) 0.1234 * 100 = 12.34

2) Truncate 처리 ----> 12

3) 12 / 100 = 0.12

 

 

반올림 / 올림도 같은 기능으로 해주면 된다.

 

 

출처 : https://is03.tistory.com/62

'C#' 카테고리의 다른 글

C# 배포시 난독화를 해야 할까? - ConfuserEx 사용법  (0) 2021.01.06
c# Task 7가지 사용법  (0) 2021.01.04
c# Windows 가상키보드 호출  (0) 2019.05.09
특수문자 포함여부  (0) 2019.03.06
클라이언트 IP 추출  (0) 2019.01.15

관련글 더보기