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 / 다른 페이지로 리다이렉트(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 / Object / String.charAt(), String.charCodeAt()

JavaScript / Object / String.charAt(), String.charCodeAt()

.charAt(), .charCodeAt() .charAt() : 특정 위치에 있는 문자를 반환합니다. .charCodeAt() : 특정 위치에 있는 문자의 유니코드 값을 반환합니다. 문법 - .charAt() string.charAt( n ) 인덱스의 값이 n인 문자를 반환합니다. 예를 들어 "12345".charAt( 2 ) 는 3입니다. 만약 인덱스 n에 문자가 없으면 빈 문자열을 반환합니다. 문법 - .charCodeAt() string.charCodeAt( n ) 인덱스의 값이 n인 문자의 유니코드 값을 반환합니다. 예를 들어 "12345".charCodeAt( 2 ) 는 3의 유니코드 ...

JavaScript / Object / String.concat() / 문자열을 이어 붙이는 메서드

JavaScript / Object / String.concat() / 문자열을 이어 붙이는 메서드

.concat()  .concat()은 문자열을 이어 붙이는 메서드입니다. 문법 string1.concat( ) string1은 필수 요소, string2 등은 선택 요소입니다. 인수가 문자열이 아닌 경우 먼저 문자열로 변환한 다음 string1에 연결합니다. 예를 들어 'ab'.concat( 'cd', 'ef' ) 는 abcdef입니다. 예제 <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; } </style> ...

JavaScript / 함수 / isFinite() / 매개변수가 유한한 수인지 검사하는 함수

JavaScript / 함수 / isFinite() / 매개변수가 유한한 수인지 검사하는 함수

isFinite() isFinite()은 매개변수가 유한값인지 검사하는 함수입니다. 문법 isFinite( value ) value : 검사할 값을 입력합니다. 유한한 숫자이면 true, 무한한 숫자 또는 숫자가 아니면 false를 반환합니다. 예제 123.123은 유한한 숫자이므로 true를 반환합니다. Infinity는 무한한 숫자이므로 false를 반환합니다. Not a Number는 문자이므로 false를 반환합니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> ...

JavaScript / Object / document.querySelector() / 특정 CSS 선택자를 가진 첫 번째 요소를 선택하는 메서드

JavaScript / Object / document.querySelector() / 특정 CSS 선택자를 가진 첫 번째 요소를 선택하는 메서드

.querySelector() .querySelector()는 CSS 선택자로 요소를 선택하게 해줍니다. 주의할 점은 선택자에 해당하는 첫번째 요소만 선택한다는 것입니다. 문법 document.querySelector( 'selector' ) 예를 들어 document.querySelector( '.xyz' ) 는 클래스 값이 xyz인 첫번째 요소에 접근합니다. 예제 1 클래스 값으로 abc를 갖는 요소 중 첫 번째 요소의 색을 빨간색으로 만듭니다. <!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> ...

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 / 사업자등록번호, 전화번호 유효성 검사하는 방법

JavaScript / 사업자등록번호, 전화번호 유효성 검사하는 방법

사업자등록번호 유효성 검사 사업자등록번호를 입력하고 전송할 폼을 만들고, 버튼 클릭 시 checkBrn() 함수를 호출한다. <input type="text" name="brn" id="brn"> <input type="button" value="Check" onclick="checkBrn();"> 메시지를 출력할 공간을 만든다. <p id="checkBrnMessage"></p> 입력한 값을 변수 brn에 담는다. var brn = document.getElementById( "brn" ).value; 정규표현식 /^{5}$/를 이용하여 유효성 검사를 한다. 000-00-00000 형식이 맞으면 OK, 그렇지 않으면 NOT OK를 출력한다. if ( /^{5}$/.test( brn ) ) ...

JavaScript / 구문(Statement) 넣는 방법

JavaScript / 구문(Statement) 넣는 방법

자바스크립트는 줄바꿈을 하면 구문(Statement)이 종료된 것으로 봅니다. var jb1 = 'Lorem' var jb2 = 'Ipsum' 하지만, 구문이 완성되지 않았다면 다음 줄까지 해석합니다. 즉 var jb1 = 'Lorem' 과 var jb1 = 'Lorem' 은 같습니다. 구문이 종료되었음을 명확히 표시할 때는 세미콜론(;)을 사용합니다. var jb1 = 'Lorem'; var jb2 = 'Ipsum'; 세미콜론 사용은 선택사항이지만, 한 줄에 여러 구문을 쓸 때는 구문과 구문 사이에 꼭 ...

JavaScript / Object / String.length / 문자열의 길이 반환하는 속성

JavaScript / Object / String.length / 문자열의 길이 반환하는 속성

.length .length는 문자열의 길이를 반환하는 속성입니다. 문법 string.length 예를 들어 'abcd'.length 는 4입니다. 예제 <!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>"1234".length : ' + '1234'.length + '</p>' ); document.write( '<p>"".length : ' + ''.length + '</p>' ); document.write( '<p>"한글".length : ' + '한글'.length + '</p>' ...

JavaScript / Object / document.title / 문서의 제목을 가져오거나 변경하는 속성

JavaScript / Object / document.title / 문서의 제목을 가져오거나 변경하는 속성

.title .title 속성으로 문서의 제목을 가져오거나 변경할 수 있습니다. 문법 1 document.title 문서의 <title> 태그의 내용을 반환합니다. 문법 2 document.title = 'ABC' 문서의 <title> 태그의 내용을 ABC로 바꿉니다. 예제 문서의 제목 JavaScript를 출력하고, 제목을 CODING FACTORY로 바꿉니다. <!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>' ...