본문 바로가기

Front-End: Web/JavaScript

[js] Math - ceil, floor, round (+ trunc)

반응형

소수점 고려

1) ceil

  • 반환: 올림한 값
  • 천장!

2) floor

  • 반환: 내림한 값
  • 바닥!

3) round

  • 반환: 반올림한 값
  • 그 중간!

공통점

  • null 넣으면 0 반환함
  • 숫자 아닌 값 넣으면 Nan 반환함

 

소수점 고려X

trunc

  • 반환: 소수점 버리고 숫자의 정수부분만
  • 위의 셋과 달리 소수점과 상관없이 단순하게 동작함

 

 

 

https://stackabuse.com/rounding-numbers-in-javascript-using-ceil-floor-and-round/

 

Rounding Numbers in JavaScript using ceil(), floor() and round()

In this guide, we'll take a look at how to round a number to an integer (whole number) in JavaScript, using ceil(), floor() and round(), with practical examples.

stackabuse.com

 

반응형