소수점 올림
document.write(Math.ceil(123.456)+"\n"); //124
document.write(Math.ceil(123.567)+"\n"); //124
소수점 버림
document.write(Math.floor(123.456)+"\n"); //124
document.write(Math.floor(123.567)+"\n"); //124
소수점 반올림 document.write(Math.round(123.456)+"\n"); //123
document.write(Math.round(123.567)+"\n"); //124
toFixed : 숫자를 문자열로 변환하면서 지정된 소수점 이하 숫자를 반올림하여 출력
document.write((123.456).toFixed(0)+"\n"); //123
document.write((123.456).toFixed(2)+"\n"); //123.46
document.write((123.456).toFixed(4)+"\n"); //123.4560
document.write((123.789).toFixed(0)+"\n"); //124
document.write((123.789).toFixed(2)+"\n"); //123.79
document.write((123.789).toFixed(4)+"\n"); //123.7890
toExponential : 숫자를 문자열로 변환하면서 소수점 앞의 숫자 하나와 지정된 개수의 소수점 이후 숫자로 구성되는 지수표기법을 사용하여 출력
document.write((123.456).toExponential(0)+"\n"); //1e+2
document.write((123.456).toExponential(2)+"\n"); //1.23e+2
document.write((123.456).toExponential(4)+"\n"); //1.2346e+2
document.write((1234.56).toExponential(0)+"\n"); //1e+3
document.write((1234.56).toExponential(2)+"\n"); //1.23e+3
document.write((1234.56).toExponential(4)+"\n"); //1.2346e+3
toPrecision : 지정된 수의 유효 숫자 개수만큼 숫자로 출력. 만약 유효 숫자 갯수가 숫자의 정수부분 전체를 출력할 만큼 충분하지 않다면 지수 표기법으로 출력
document.write((123.456).toExponential(2)+"\n"); //1.23e+2
document.write((123.456).toExponential(4)+"\n"); //1.2346e+2
document.write((123.456).toExponential(7)+"\n"); //1.2345600e+2
'Dog Feet Developer > JavaScript' 카테고리의 다른 글
[JavaScript] 오늘날짜 구하기 (YYYMMDD,YYYY/MM/DD,YYYY-MM-DD) (0) | 2021.01.26 |
---|---|
[JavaScript/JQuery] Input checkbox 컨트롤 (0) | 2020.05.18 |