JavaScript / 내림, 올림, 반올림

자바스크립트에서 숫자를 내림할 때는 Math.floor(), 올림할 때는 Math.ceil(), 반올림할 때는 Math.round()를 사용합니다.

Math.floor() - 내림

Math.floor()는 어떤 수보다 크지 않은 최대의 정수를 반환합니다.

문법

Math.floor( Number )

Number에는 숫자가 들어갑니다.

예제

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>JavaScript</title>
    <style>
      body {
        font-family: Consolas, monospace;
      }
    </style>
  </head>
  <body>
    <script>
      document.write( '<p>Math.floor( 1.4 ) : ' + Math.floor( 1.4 ) + '</p>' );
      document.write( '<p>Math.floor( 1.5 ) : ' + Math.floor( 1.5 ) + '</p>' );
      document.write( '<p>Math.floor( 1.6 ) : ' + Math.floor( 1.6 ) + '</p>' );
      document.write( '<p>Math.floor( -1.4 ) : ' + Math.floor( -1.4 ) + '</p>' );
      document.write( '<p>Math.floor( -1.5 ) : ' + Math.floor( -1.5 ) + '</p>' );
      document.write( '<p>Math.floor( -1.6 ) : ' + Math.floor( -1.6 ) + '</p>' );
    </script>
  </body>
</html>

Math.ceil() - 올림

Math.ceil()는 어떤 수보다 작지 않은 최소의 정수를 반환합니다.

문법

Math.ceil( Number )

Number에는 숫자가 들어갑니다.

예제

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>JavaScript</title>
    <style>
      body {
        font-family: Consolas, monospace;
      }
    </style>
  </head>
  <body>
    <script>
      document.write( '<p>Math.ceil( 1.4 ) : ' + Math.ceil( 1.4 ) + '</p>' );
      document.write( '<p>Math.ceil( 1.5 ) : ' + Math.ceil( 1.5 ) + '</p>' );
      document.write( '<p>Math.ceil( 1.6 ) : ' + Math.ceil( 1.6 ) + '</p>' );
      document.write( '<p>Math.ceil( -1.4 ) : ' + Math.ceil( -1.4 ) + '</p>' );
      document.write( '<p>Math.ceil( -1.5 ) : ' + Math.ceil( -1.5 ) + '</p>' );
      document.write( '<p>Math.ceil( -1.6 ) : ' + Math.ceil( -1.6 ) + '</p>' );
    </script>
  </body>
</html>

Math.round() - 반올림

Math.round()는 어떤 수와 가장 가까운 정수를 반환합니다. 어떤 수의 소수 부분이 0.5인 경우 가까운 큰 정수를 반환합니다.

문법

Math.round( Number )

Number에는 숫자가 들어갑니다.

예제

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>JavaScript</title>
    <style>
      body {
        font-family: Consolas, monospace;
      }
    </style>
  </head>
  <body>
    <script>
      document.write( '<p>Math.round( 1.4 ) : ' + Math.round( 1.4 ) + '</p>' );
      document.write( '<p>Math.round( 1.5 ) : ' + Math.round( 1.5 ) + '</p>' );
      document.write( '<p>Math.round( 1.6 ) : ' + Math.round( 1.6 ) + '</p>' );
      document.write( '<p>Math.round( -1.4 ) : ' + Math.round( -1.4 ) + '</p>' );
      document.write( '<p>Math.round( -1.5 ) : ' + Math.round( -1.5 ) + '</p>' );
      document.write( '<p>Math.round( -1.6 ) : ' + Math.round( -1.6 ) + '</p>' );
    </script>
  </body>
</html>

내림, 올림, 반올림하는 자릿수 변경하기

Math.floor(), Math.ceil(), Math.round()는 정수로 만드는 것이므로, 소수 첫째 자리에서 내림, 올림, 반올림합니다. 만약 내림, 올림, 반올림하는 자릿수를 변경하고 싶다면 곱하기와 나누기를 이용합니다.

예를 들어 소수 둘째 자리에서 반올림하고 싶다면 10을 곱하여 반올림한 후 10으로 나눕니다.

Math.round( 1.25 * 10 ) / 10  // 1.3

백의 자리에서 반올림하고 싶다면 1000으로 나누고 반올림한 후 1000을 곱합니다.

Math.round( 1234567 / 1000 ) * 1000  // 1235000

 

같은 카테고리의 다른 글
JavaScript / 함수 / String() - 숫자를 문자열로 변환하는 함수

JavaScript / 함수 / String() - 숫자를 문자열로 변환하는 함수

자바스크립트의 String()은 숫자를 문자열로 변환하는 함수입니다. 문자열을 숫자로 변환할 때는 Number() 함수를 사용합니다.

JavaScript / 숫자에 천 단위 쉼표 추가하는 방법, 제거하는 방법

JavaScript / 숫자에 천 단위 쉼표 추가하는 방법, 제거하는 방법

큰 숫자인 경우 천 단위마다 쉼표(콤마)를 추가하는 경우가 많습니다. 하지만 계산을 위해서는 쉼표를 제거해야 합니다. 자바스크립트로 숫자에 쉼표를 추가하는 방법, 쉼표가 있는 숫자에서 쉼표를 제거하는 방법을 알아보겠습니다.

JavaScript / 반복문 / while, do - while, for

JavaScript / 반복문 / while, do - while, for

자바스크립트 반복문에는 while, do - while, for 세가지가 있습니다. 각 방식으로 0부터 9까지 출력해 보겠습니다.

JavaScript / 내림, 올림, 반올림

JavaScript / 내림, 올림, 반올림

자바스크립트에서 숫자를 내림할 때는 Math.floor(), 올림할 때는 Math.ceil(), 반올림할 때는 Math.round()를 사용합니다.

JavaScript / 함수 / Number() - 문자열을 숫자로 변환하는 함수

JavaScript / 함수 / Number() - 문자열을 숫자로 변환하는 함수

자바스크립트의 Number()는 문자열을 숫자로 변환하는 함수입니다. 숫자를 문자열로 변환할 때는 String() 함수를 사용합니다.

JavaScript / 조건문 / if, else if, else, switch

JavaScript / 조건문 / if, else if, else, switch

특정 조건 만족 시 어떤 작업을 수행하고 싶을 때 사용하는 것이 조건문입니다. 크게 if문과 switch문으로 구분할 수 있습니다. 조건식에서 비교할 값이 많을 때 switch문을 사용합니다.

JavaScript / 메모

JavaScript / 메모

천단위 쉼표 (123456789).toLocaleString() // 123,456,789 인코딩 함수, 디코딩 함수 인코딩 함수 escape() encodeURI() encodeURIComponent() 디코딩 함수 unescape() decodeURI() decodeURIComponent() IE8에서 last-child, nth-child 등 가상 클래스 적용시키는 방법 selectivizr HTML 문서의 style 태그 안에 넣은 CSS 코드에는 적용되지 않는다. 외부 CSS 파일에만 적용됩니다. jQuery, prototype 등 다른 자바스크립트 라이브러리가 필요하다.