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>