라이브러리

[JAVASCRIPT] Math.round(x) - 반올림하여 정수 반환




Math.round(x)란?


Math.round(x) 함수는 주어진 숫자 x를 반올림하여 가장 가까운 정수에 근사하는 함수입니다.

Math.round(x) 사용법


Math.round(x) 함수는 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
javascript

Math.round(x)



Math.round(x) 예제


# 예제 1: Math.round(x) 사용하기


#hostingforum.kr
javascript

let x = 3.7;

console.log(Math.round(x));  // 결과: 4



let y = -3.7;

console.log(Math.round(y));  // 결과: -4



# 예제 2: Math.round(x)와 반올림


Math.round(x) 함수는 반올림을 수행합니다. 따라서, 3.5와 3.6은 4로 반올림됩니다.

#hostingforum.kr
javascript

let x = 3.5;

console.log(Math.round(x));  // 결과: 4



let y = 3.6;

console.log(Math.round(y));  // 결과: 4



# 예제 3: Math.round(x)와 소수점 이하 자릿수


Math.round(x) 함수는 소수점 이하 자릿수를 고려하지 않습니다. 따라서, 3.49와 3.51은 3으로 반올림됩니다.

#hostingforum.kr
javascript

let x = 3.49;

console.log(Math.round(x));  // 결과: 3



let y = 3.51;

console.log(Math.round(y));  // 결과: 4



Math.round(x)와 Math.floor(x), Math.ceil(x)


Math.round(x) 함수는 Math.floor(x) 함수와 Math.ceil(x) 함수와 함께 사용할 수 있습니다.

- Math.floor(x) 함수는 x를 내림하여 가장 가까운 정수에 근사합니다.
- Math.ceil(x) 함수는 x를 올림하여 가장 가까운 정수에 근사합니다.

#hostingforum.kr
javascript

let x = 3.7;

console.log(Math.floor(x));  // 결과: 3

console.log(Math.ceil(x));   // 결과: 4

console.log(Math.round(x));  // 결과: 4



결론


Math.round(x) 함수는 주어진 숫자 x를 반올림하여 가장 가까운 정수에 근사하는 함수입니다. Math.round(x) 함수는 Math.floor(x) 함수와 Math.ceil(x) 함수와 함께 사용할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 6 페이지

검색

게시물 검색