JavaScript / Object / 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.6 ) : ' + Math.floor( 1.6 ) + '</p>' );
      document.write( '<p>Math.floor( 1.2 ) : ' + Math.floor( 1.2 ) + '</p>' );
      document.write( '<p>Math.floor( 1.0 ) : ' + Math.floor( 1.0 ) + '</p>' );
      document.write( '<p>Math.floor( -1.2 ) : ' + Math.floor( -1.2 ) + '</p>' );
      document.write( '<p>Math.floor( -1.6 ) : ' + Math.floor( -1.6 ) + '</p>' );
      document.write( '<p>Math.floor( -2.0 ) : ' + Math.floor( -2.0 ) + '</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.6 ) : ' + Math.ceil( 1.6 ) + '</p>' );
      document.write( '<p>Math.ceil( 1.2 ) : ' + Math.ceil( 1.2 ) + '</p>' );
      document.write( '<p>Math.ceil( 1.0 ) : ' + Math.ceil( 1.0 ) + '</p>' );
      document.write( '<p>Math.ceil( -1.2 ) : ' + Math.ceil( -1.2 ) + '</p>' );
      document.write( '<p>Math.ceil( -1.6 ) : ' + Math.ceil( -1.6 ) + '</p>' );
      document.write( '<p>Math.ceil( -2.0 ) : ' + Math.ceil( -2.0 ) + '</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.6 ) : ' + Math.round( 1.6 ) + '</p>' );
      document.write( '<p>Math.round( 1.5 ) : ' + Math.round( 1.5 ) + '</p>' );
      document.write( '<p>Math.round( 1.2 ) : ' + Math.round( 1.2 ) + '</p>' );
      document.write( '<p>Math.round( 1.0 ) : ' + Math.round( 1.0 ) + '</p>' );
      document.write( '<p>Math.round( -1.2 ) : ' + Math.round( -1.2 ) + '</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( -2.0 ) : ' + Math.round( -2.0 ) + '</p>' );
    </script>
  </body>
</html>

같은 카테고리의 다른 글
JavaScript / Plugin / Swiper / 슬라이더 플러그인

JavaScript / Plugin / Swiper / 슬라이더 플러그인

Swiper는 널리 사용되는 슬라이더 플러그인 중의 하나입니다. 다양한 옵션을 제공하며, 해상도에 따라 다른 옵션을 줄 수 있어 반응형 사이트에 사용하기 좋습니다. 홈페이지 : https://swiperjs.com/ GitHub : https://github.com/nolimits4web/swiper Swiper는 IE를 지원하지 않습니다. 만약 IE 지원이 중요하다면, IE를 지원하는 Swiper 구 버전을 사용하거나 다른 슬라이더를 사용하세요. 기본 사용법 스크립트 파일과 CSS 파일을 연결합니다. 다음은 CDN을 이용하여 연결하는 코드입니다. <script src="https://unpkg.com/swiper@7/swiper-bundle.min.js"></script> <link ...

JavaScript / 함수 / encodeURI(), encodeURIComponent(), decodeURI(), decodeURIComponent()

JavaScript / 함수 / encodeURI(), encodeURIComponent(), decodeURI(), decodeURIComponent()

encodeURI(), encodeURIComponent(), decodeURI(), decodeURIComponent() encodeURI() : 인터넷 주소에서 사용하는 :, ;, /, =, ?, & 등을 제외하고 인코딩하는 함수입니다. encodeURIComponent() : 모든 문자를 인코딩하는 함수입니다. decodeURI() : encodeURI()로 인코딩한 문자열을 디코딩하는 함수입니다. decodeURIComponent() : encodeURIComponent()로 인코딩한 문자열을 디코딩하는 함수입니다. 문법 encodeURI( uri ) encodeURIComponent( uri ) decodeURI( uri ) decodeURIComponent( uri ) uri : 인코딩하려는 인터넷 주소를 입력합니다. 예제 특수문자와 한글을 포함한 URI를 인코팅, ...

JavaScript / 조건문 / switch

JavaScript / 조건문 / switch

가장 기본적인 조건문은 if입니다. 하지만 조건식에서 비교할 값이 많다면 코드가 길어지고 가독성이 떨어진다는 단점이 있습니다. 이럴 땐 switch를 사용하는 것이 좋습니다. 문법 switch ( condition ) { case value1: statement1; break; case value2: statement2; break; ... ...

JavaScript / Event / onsubmit / 폼 전송하기 전에 작업 수행하게 하는 이벤트

JavaScript / Event / onsubmit / 폼 전송하기 전에 작업 수행하게 하는 이벤트

JavaScript의 onsubmit 이벤트를 이용하면 폼의 값을 전송하기 전에 어떤 작업을 하게 할 수 있습니다. 간단한 예제로 어떻게 작동하는지 알아보겠습니다. 예제 1 간단한 회원 가입 페이지를 만듭니다. 전송 버튼은 input 태그로 만듭니다. Register를 클릭하면 ok.html로 이동합니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> ...

JavaScript / Object / Array.splice() / 배열에서 특정 범위의 값들을 추출하고, 그 자리에 새로운 값을 넣는 속성

JavaScript / Object / Array.splice() / 배열에서 특정 범위의 값들을 추출하고, 그 자리에 새로운 값을 넣는 속성

.splice() .splice()는 배열에서 특정 범위의 값들을 추출하고, 그 자리에 새로운 값을 넣습니다. 문법 array.splice( start, count ) 예를 들어 jbAry.splice( 1, 2 )는 jbAry  배열의 두번째 값부터 2개를 추출합니다. array.splice( start, count, value1, value2, ... ) 예를 들어 jbAry.splice( 1, 2, 'abc', 'def' )는 jbAry  배열의 두번째 값부터 2개를 추출하고, 그 자리에 abc, def 두 값을 ...

JavaScript / Object / String.fromCharCode() / 유니코드를 문자로 바꾸는 메서드

JavaScript / Object / String.fromCharCode() / 유니코드를 문자로 바꾸는 메서드

.fromCharCode() .fromCharCode()는 유니코드를 문자로 바꾸는 메서드입니다. 문법 string.charCodeAt( index ) 예제 1 'HELLO'.charCodeAt( 0 ) H의 유니코드 72를 반환합니다. 예제 2 <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; } </style> </head> <body> <script> var jb = 'HELLO'; for ( i = 0; i < jb.length; i++ ) { document.write ( jb.charCodeAt( i ...

JavaScript / Object / document.getElementById()

JavaScript / Object / document.getElementById()

.getElementById() .getElementById()는 id의 값으로 특정한 값을 가진 요소를 가져옵니다. 문법 document.getElementById( 'id' ) id에는 가져오려는 요소의 id의 값을 넣습니다. 예를 들어 var jb = document.getElementById( 'xyz' ); 는 id의 값이 xyz인 요소를 가져와서 변수 jb에 저장합니다. 예제 id의 값이 ab인 요소를 가져와서 몇 가지를 출력하는 예제입니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> ...

JavaScript / 다른 페이지로 리다이렉트(Redirect) 하기

JavaScript / 다른 페이지로 리다이렉트(Redirect) 하기

자바스크립트를 이용해서 특정 URL로 접속했을 때 다른 URL로 이동시킬 수 있다. 다른 URL로 이동시키는 것은 window.location.href 를 이용한다. 예를 들어 window.location.href = 'http://www.abc.com/'; 은 웹브라우저로 접속했을 때 http://www.abc.com/으로 이동시킨다. 만약 http://www.abc.com/으로 접속했을 때 http://www.codingfactory.net/로 이동시키고 싶다면 다음과 같이 한다. if ( window.location == 'http://www.abc.com/' ) { window.location.href='http://www.codingfactory.net/'; } PHP / 다른 페이지로 리디렉션(Redirection)하는 방법

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

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

String() String()은 숫자를 문자열로 변환하는 함수입니다. 문법 String( object ) object : 숫자 또는 숫자를 값으로 하는 변수 등을 입력합니다. 예제 변수 c의 값은 숫자 1과 숫자 2를 더한 3입니다. 변수 d의 값은 문자 1과 문자 2를 더한 12입니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> ...

JavaScript / 다음(카카오) 우편번호 검색 서비스 사용하는 방법

JavaScript / 다음(카카오) 우편번호 검색 서비스 사용하는 방법

다음(카카오)에서 우편번호 검색 서비스를 무료로 제공한다. 팝업 창에 Powerd by kakao가 표시되고, 기본 모양이 투박하다는 단점이 있지만, 빠르게 주소 검색 서비스를 구현해주는 고마운 서비스임에는 틀림이 없다. 자세한 사용법은 아래 링크에 있다. https://postcode.map.daum.net/guide 너무 상세한 설명이라 이해가 어렵다면, 최소한의 코드로 만들고, 필요한 것을 하나씩 추가하는 게 좋다. 기본 예제와 작동 방식 아래는 최소한의 코드로 작성한 것이다. ...